0

I am trying to parse into a string:

DateTime datWeekDB = DateTime.ParseExact(weekDB.Value, "WyyyyMMdd", CultureInfo.InvariantCulture);

The text strings are like, eg:

W20220120

But this is failing:

  <LogEntry Date="2022-01-20 14:51:09" Severity="Exception" Source="MSAToolsLibrary.Importer.Importer.ImportHistoryFromGeneric" ThreadId="1">
    <Exception Type="System.FormatException" Source="MSAToolsLibrary.Importer.Importer.AddWeekToHistory">
      <Message>String was not recognized as a valid DateTime.</Message>
      <StackTrace>   at MSAToolsLibrary.Importer.Importer.AddWeekToHistory(XDocument&amp; xdoc, MSAHistoryWeek historyWeek, DetectStudentItemDescriptionAndTypeDelegate funcDetectStudentItemDescriptionAndType, Boolean bSort) in D:\My Programs\2022\MSAToolsLibrary\MSAToolsLibrary\Importer\Importer.cs:line 275
   at MSAToolsLibrary.Importer.Importer.ImportHistoryFromGeneric() in D:\My Programs\2022\MSAToolsLibrary\MSAToolsLibrary\Importer\Importer.cs:line 146</StackTrace>
    </Exception>
  </LogEntry>

The first character W is fixed and will always precede the YYYYMMDD text. How do I circumnavigate this issue?

I have tried to research the correct use of this API call for when there is generic text in the format string. But not spotted anything.


Update

I will most likely rephrase the question. Since in the comments they stated that it should work, I thought I would confirm the actual text being passed in to parse. I have thie XElement variable called weekDB which I try to get the node name from:

DateTime datWeekDB = DateTime.ParseExact(weekDB.Value, "WyyyyMMdd", CultureInfo.InvariantCulture);

I make the call weekDB.Value but it is returning a concatenated string of all the inner text values. I was hoping it would return the name of the actual XElement node. How do I fix that?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 2
    Remove the W before you parse it. – Jen R Jan 20 '22 at 14:56
  • 5
    I have tried it and it parses successsfully W20220120 to 01/20/2022 00:00:00 – Tim Schmelter Jan 20 '22 at 14:59
  • @TimSchmelter I am just checking something then, to see if my issue lies elsewhere. One moment. – Andrew Truckle Jan 20 '22 at 15:00
  • 2
    Your code is failing somewhere else or on a different value. As @TimSchmelter pointed out the code in the question works, see this repo: https://dotnetfiddle.net/c11Oqs – Igor Jan 20 '22 at 15:02
  • @Igor I have added some code to teh question explaining where the problem lies . It is related to how I am trying to get the name of my `XElement` node. It is that text string that should be `WYYYYMMDD`. – Andrew Truckle Jan 20 '22 at 15:06
  • 2
    Regarding your edit - so `weekDB` is an element and you want the name of that element? e.g. `some text here`? if so, you want `weekDB.Name.LocalName`, not `weekDB.Value`. – D Stanley Jan 20 '22 at 15:06
  • @DStanley Yes you understand correctly. – Andrew Truckle Jan 20 '22 at 15:07
  • 1
    @AndrewTruckle: Does this answer your question? https://stackoverflow.com/questions/15336352/how-to-get-element-names-from-xelement-using-c – Tim Schmelter Jan 20 '22 at 15:07
  • @TimSchmelter Thanks for the link. I am trying that out now. – Andrew Truckle Jan 20 '22 at 15:09
  • This is where the debugger is invaluable. If you had put a breakpoint on that line and looked at the inputs it would have been obvious that the problem was not the date parsing but the value that you were trying to parse. – D Stanley Jan 20 '22 at 15:11
  • @DStanley: assumes that he can actually debug that code – Tim Schmelter Jan 20 '22 at 15:12
  • @DStanley I realise that. But this is a c# COM DLL I am running from c++ MFC - it gets the DLL to read teh XML file and do some stuff. I understand debugging in the C++ MFC main app environment but not when it enters the COM DLL context. – Andrew Truckle Jan 20 '22 at 15:12
  • @TimSchmelter The linked answer resolved the issue. – Andrew Truckle Jan 20 '22 at 15:13

0 Answers0