The string '3/18/09 10:16 PM' is not a val

2019-01-11 21:13发布

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# xml parsing
2条回答
ゆ 、 Hurt°
2楼-- · 2019-01-11 21:58

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

2009-03-13T22:16:00
查看更多
干净又极端
3楼-- · 2019-01-11 22:07

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...

查看更多
登录 后发表回答