Jerry Nixon
Jerry Nixon

Reputation: 31823

How to de-serialize XML with many child nodes

If my XML is like this:

<Item>
    <Name>Jerry</Name>
    <Array>
        <Item>
            <Name>Joe</Name>
        </Item>
        <Item>
            <Name>Sam</Name>
        </Item>
    </Array>
</Item>

I can serialize it into this class:

[DataContract(Namespace = "", Name = "dict")]
public class Item
{
    [DataMember(Name = "Name")]
    public string Name { get; set; }
    [DataMember(Name = "Array")]
    public IEnumerable<Item> Children { get; set; }
}

But what if my XML is like this?

<Item>
    <Name>Jerry</Name>
    <Item>
        <Name>Joe</Name>
    </Item>
    <Item>
        <Name>Sam</Name>
    </Item>
</Item>

This does not work:

[DataContract(Namespace = "", Name = "Item")]
public class Item
{
    [DataMember(Name = "Name")]
    public string Name { get; set; }
    [DataMember(Name = "Item")]
    public IEnumerable<Item> Children { get; set; }
}

What is the right way to decorate the class?

Upvotes: 5

Views: 2468

Answers (2)

George Mamaladze
George Mamaladze

Reputation: 7931

Try using CollectionDataContractAttribute Internally you should create a generic list of Items and decorate it as CollectionDataContractAttribute with appropriate parameters.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063013

If you want control over xml, then XmlSerializer is the way to go, not DataContractSerializer. The latter simply lacks the fine-grained control that XmlSerializer offers; in this case for things like list/array layout - but even just xml attributes are a problem for DataContractSerializer. Then it is just:

public class Item {
    public string Name { get; set; }
    [XmlElement("Item")]
    public List<Item> Children { get; set; }
}
public class Item {
    public string Name { get; set; }
}

and:

var serializer = new XmlSerializer(typeof(Item));
var item = (Item) serializer.Deserialize(source);

Upvotes: 5

Related Questions