The string '3/18/09 10:16 PM' is not a valid AllXsd value

C#XmlParsing

C# Problem Overview


Obviously the reader doesn't like this format incoming from the response XML.

Wondering if I can reformat this. Trying to convert to DateTime using the following code with my XmlReader:

reader.ReadContentAsDateTime();

C# Solutions


Solution 1 - C#

According to the XML schema spec, date time values should be in ISO8601 format, e.g., something like

2009-03-13T22:16:00

Solution 2 - C#

Xml readers generally expect dates/times in a very specific format; you can use this yourself using XmlConvert:

string s = XmlConvert.ToString(DateTime.Now);
DateTime when = XmlConvert.ToDateTime(s);

If you are using something else, you'll have to read it as a string and use DateTime.TryParseExact (or similar) to specify the actual format string:

string s = reader.ReadContentAsString();
DateTime when = DateTime.ParseExact(s, "M/d/yy hh:mm tt",
     CultureInfo.InvariantCulture);

If you are using XmlSerializer, you could use a shim property to do the conversion - let me know if this is what you are doing...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser72603View Question on Stackoverflow
Solution 1 - C#David NormanView Answer on Stackoverflow
Solution 2 - C#Marc GravellView Answer on Stackoverflow