C# 字符串 '3/18/09 10:16 PM' 不是有效的 AllXsd 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/661881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The string '3/18/09 10:16 PM' is not a valid AllXsd value
提问by user72603
Obviously the reader doesn't like this format incoming from the response XML.
显然,读者不喜欢这种从响应 XML 传入的格式。
Wondering if I can reformat this. Trying to convert to DateTime
using the following code with my XmlReader
:
想知道我是否可以重新格式化这个。尝试转换为DateTime
使用以下代码与我的XmlReader
:
reader.ReadContentAsDateTime();
采纳答案by Marc Gravell
Xml readers generally expect dates/times in a very specific format; you can use this yourself using XmlConvert
:
Xml 阅读器通常期望以非常特定的格式获取日期/时间;你可以自己使用它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:
如果您正在使用其他东西,则必须将其作为字符串读取并使用DateTime.TryParseExact
(或类似的)来指定实际的格式字符串:
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...
如果您正在使用XmlSerializer
,则可以使用 shim 属性进行转换 - 如果这就是您正在做的,请告诉我...