【发布时间】:2021-03-28 02:27:08
【问题描述】:
要反序列化的XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<changes next="296">
<change>
<objectid>E702C43C-E04B-450B-BEBC-76646AB299C5</objectid>
<parentid>ED98C97F-A202-48ED-AEEA-34362508A30B</parentid>
<objecttype>file</objecttype>
<listentype>remove</listentype>
</change>
<change>
<objectid>3A242975-CEF0-432B-A997-B33D85C138C8</objectid>
<parentid>ED98C97F-A202-48ED-AEEA-34362508A30B</parentid>
<objecttype>file</objecttype>
<listentype>add</listentype>
</change>
</changes>
使用的数据模型:
[XmlRoot("changes")]
public class ChangeListener
{
public List<Change> Changes { get; set; }
}
[XmlRoot("change")]
public class Change
{
[XmlElement("objectid")]
public Guid objectid { get; set; }
[XmlElement("parentid")]
public Guid parentid { get; set; }
[XmlElement("objecttype")]
public string objecttype { get; set; }
[XmlElement("listentype")]
public string listentype { get; set; }
}
反序列化代码,这里的结果是上面xml的字符串格式:
(ChangeListener)new XmlSerializer(typeof(ChangeListener)).Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)))
但我收到此代码的错误;我也尝试了几件事,例如将ChangeListener 类的Changes 属性标记为[XmlElement("changes")],而不是将类标记为xmlroot,但它也不起作用。
请提出解决此问题的好方法。
【问题讨论】:
-
“我遇到了错误” - 但您并没有告诉我们什么错误。请编辑您的问题并添加细节。如果您进行了更改,请具体说明更改并将具体错误与您尝试的代码版本相匹配。
-
这是未格式化的异常,但在我的情况下,rich-n 提供的答案之一可以正常工作。
标签: c# xml deserialization