我正在尝试反序列化从服务调用中获得的一些响应。但是,响应包含一些非标准的日期时间格式。它的投掷字符串'13:37:53 2012年2月29日PST'不是有效的AllXsd值。我想知道是否有办法可以更改我的xmlreader或其他东西,以便我可以反序列化它?
<TimeCreated xsi:type="xs:dateTime">13:37:53 Feb 29, 2012 PST</TimeCreated>
<TimeUpdated xsi:type="xs:dateTime">13:37:53 Feb 29, 2012 PST</TimeUpdated>
这是我用来反序列化的代码。
public static T DeserializeFromXml<T>(string xml)
{
T result;
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader txReader = new StringReader(xml))
{
// Create XmlReaderSettings
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
// Create a new NameTable
NameTable nt = new NameTable();
// Create a new NamespaceManager
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
// Add your namespaces used in the XML
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
// Create the XmlParserContext using the previous declared XmlNamespaceManager
XmlParserContext ctx = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
// Instantiate a new XmlReader, using the previous declared XmlReaderSettings and XmlParserContext
XmlReader reader = XmlReader.Create(txReader, settings, ctx);
result = (T)serializer.Deserialize(reader);
}
return result;
}