Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlSerializer.Deserialize on a List<> item

I've tried all the solutions I could find on SO and elsewhere, but can't seem to figure out why this is not working.

Straightforward deserialization of an XML string into an object, the object has one property - a List:

[XmlTypeAttribute(AnonymousType = true)]
public class UpdateData
{
    [XmlArrayItem(ElementName = "Updates")]
    public List<Update> Updates { get; set; }

    public UpdateData()
    {
        Updates = new List<Update>();
    }

}

public class Update
{
    [XmlElement(ElementName = "MemberID")]
    public int MemberID { get; set; }

    [XmlElement(ElementName = "AnalysisID")]
    public int AnalysisID { get; set; }

    [XmlElement(ElementName = "MemberName")]
    public string MemberName { get; set; }

    [XmlElement(ElementName = "RecordDate")]
    public DateTime RecordDate { get; set; }
}

Here is the deserialize code:

private object DeserialzeXml(string xml)
{
    var xmlSer = new XmlSerializer(typeof(UpdateData), new XmlRootAttribute("UpdateData"));
    var stringReader = new StringReader(xml);
    return xmlSer.Deserialize(stringReader);
}

And here is the XML:

<?xml version="1.0" encoding="utf-8" ?> 
<UpdateData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Updates>
        <Update>
            <MemberID>1</MemberID> 
            <AnalysisID>1</AnalysisID> 
            <MemberName>XXXXXXXXXXXXX</MemberName> 
        </Update>
        <Update>
            <MemberID>1</MemberID> 
            <AnalysisID>2</AnalysisID> 
            <MemberName>YYYYYYYYYYYYY</MemberName> 
        </Update>
        <Update>
            <MemberID>1</MemberID> 
            <AnalysisID>3</AnalysisID> 
            <MemberName>ZZZZZZZZZZZZ</MemberName> 
        </Update>
    </Updates>
</UpdateData>

This code compiles and runs, and returns an object of type UpdateData, but the Updates property is empty. Any ideas?

like image 584
staterium Avatar asked Jan 18 '10 14:01

staterium


People also ask

How do I deserialize in C#?

NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

Can I make XmlSerializer ignore the namespace on Deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

What does deserialize XML mean?

Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Deserialization, on the other hand, is used to convert the byte of data, such as XML or binary data, to object type.


1 Answers

Try changing the attributes on your list to this:

[XmlArray(ElementName="Updates")]
[XmlArrayItem(ElementName="Update")]
public List<Update> Updates { get; set; }
like image 126
Brian Ensink Avatar answered Oct 15 '22 14:10

Brian Ensink