简体   繁体   English

C# XML 反序列化问题

[英]C# XML deserialization issue

XML to be deserialized: 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>

Data models used:使用的数据模型:

[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; }
}

Deserialization code, here result is above xml in string format:反序列化代码,这里的结果是字符串格式的 xml 上面:

(ChangeListener)new XmlSerializer(typeof(ChangeListener)).Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(result)))

But I am getting errors for this code;但是我收到此代码的错误; I also tried couple of things eg marked Changes property of ChangeListener class with [XmlElement("changes")] instead of marking class as xmlroot but it also did not worked.我还尝试了几件事,例如用[XmlElement("changes")]标记ChangeListener class 的Changes属性,而不是将 class 标记为 xmlroot,但它也没有工作。

Kindly suggest good approach for this issues.请为这个问题提出好的方法。

The Class for mentioned XML should look like below.提到的 XML 的 Class 应如下所示。

[XmlRoot(ElementName="change")]
    public class Change {
        [XmlElement(ElementName="objectid")]
        public string Objectid { get; set; }
        [XmlElement(ElementName="parentid")]
        public string Parentid { get; set; }
        [XmlElement(ElementName="objecttype")]
        public string Objecttype { get; set; }
        [XmlElement(ElementName="listentype")]
        public string Listentype { get; set; }
    }

    [XmlRoot(ElementName="changes")]
    public class Changes {
        [XmlElement(ElementName="change")]
        public List<Change> Change { get; set; }
        [XmlAttribute(AttributeName="next")]
        public string Next { get; set; }
    }

The problem is that the Changes List in the ChangeListener is confusing the serializer because there's nothing called 'Changes' in the XML.问题是 ChangeListener 中的更改列表混淆了序列化程序,因为 XML 中没有任何称为“更改”的内容。

The only change we need to make is to annotate the declaration of Changes with [XmlElement("change")] as below:我们需要做的唯一更改是使用[XmlElement("change")]注释 Changes 的声明,如下所示:

[XmlRoot("changes")]
public class ChangeListener
{
    [XmlElement("change")]
    public List<Change> Changes { get; set; }
}

The XML shown then deserializes correctly.然后显示的 XML 会正确反序列化。

Try changing the type of objectid and parentid from Guid to string.尝试将 objectid 和 parentid 的类型从 Guid 更改为字符串。 Share the error details if you still get errors.如果您仍然遇到错误,请分享错误详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM