简体   繁体   中英

C# XML deserialization issue

XML to be deserialized:

<?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:

(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.

Kindly suggest good approach for this issues.

The Class for mentioned XML should look like below.

[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.

The only change we need to make is to annotate the declaration of Changes with [XmlElement("change")] as below:

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

The XML shown then deserializes correctly.

Try changing the type of objectid and parentid from Guid to string. Share the error details if you still get errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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