views:

27

answers:

1

Hi.

Is there a way to convert different named xml nodes into one class when deserializing an XML

Example XML:

<items>
    <aaa>value</aaa>
    <bbb>value</bbb>
</items>

Normaly i would write:

[XmlRoot("items")]
class Items
{
    [XmlElement("aaa")]
    public string aaa;

    [XmlElement("bbb")]
    public string bbb;
}

But now i would like to do something like this

[XmlRoot("items")]
class Items
{
    [XmlElement("aaa")]
    [XmlElement("bbb")]
    public List<string> item;
}

Here I would love if "aaa" and "bbb" was added to the same list.

A: 

You cannot do this during deserialization - imagine how this second objects of yours would be serialized out to XML if that approach of your were possible.... how on earth would the XML serializer know whether and when to use "aaa" or "bbb" as the element tag......

What you can do is do the straight serialization first into an XML serializable object type (with aaa and bbb properties), and then if you really must, create a separate object that contains a List<string> and stick all your data in there.

marc_s