【发布时间】:2018-06-28 20:11:43
【问题描述】:
我的xml字符串如下
"<?xml version=\"1.0\" encoding=\"utf-8\"?><p:Msg xmlns:tns=\"http://xyx.com\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.xyx.com/location/921.xsd\"><Header><Type>P:B2</Type><UserID>MARKISCOOL</UserID><MsgID>4213</MsgID>
</Header><Data><StatusRecord><TimestampUTCCurrent hex=\"40B18261\" intermediate=\"1085375073\" uom=\"\">2016-01-20T06:05:55Z</TimestampUTCCurrent>
<FileType hex=\"00002765\" intermediate=\"10003\" uom=\"\">10003</FileType>
</StatusRecord></Data></p:Msg>"
我必须将这个 xml 字符串反序列化为一个对象,如图所示
[XmlRoot(Namespace = "http://xyx.com", ElementName = "Msg", IsNullable = true)]
public class Info
{
public string Type { get; set; }
public string UserID { get; set; }
[XMlElement("MsgID")]
public int MessageId { get; set; }
public string TimestampUTCCurrent { get; set; }
public int FileType { get; set; }
}
我正在尝试将 xml 字符串反序列化为 Info 类,但我将 null 值放入 Info 类。我不确定为什么 xml 中的值没有复制到“Info”对象中。
public Info Deserialize(string xmlString)
{
XDocument doc = XDocument.Parse(xmlString);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Info));
using (var reader = doc.Root.CreateReader())
{
return (Info)xmlSerializer.Deserialize(reader);
}
}
【问题讨论】:
-
I am getting error。为什么不与我们分享错误?最高机密? -
System.Xml.XmlException: '根级别的数据无效。第 1 行,位置 1。'
标签: c# xml xml-parsing xml-deserialization