开发者

Deserializing different named xml nodes

开发者 https://www.devze.com 2023-01-03 07:22 出处:网络
Is there a way to convert different named xml nodes into one class when deserializing an XML Example XML:

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<str开发者_如何学Cing> item;
}

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消