简体   繁体   English

反序列化不同的命名xml节点

[英]Deserializing different named xml nodes

Is there a way to convert different named xml nodes into one class when deserializing an XML 反序列化XML时,是否有一种方法可以将不同的命名xml节点转换为一个类

Example XML: 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. 在这里,我想将“ aaa”和“ bbb”添加到同一列表中。

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...... 您无法在反序列化过程中执行此操作-设想一下,如果可以采用您的第二种方法,那么如何将您的第二个对象序列化为XML .... XML序列化程序如何知道是否以及何时使用“ aaa”或“ bbb” “作为元素标签...

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. 您可以做的是先将直接序列化为XML可序列化的对象类型(具有aaabbb属性),然后,如果确实需要,创建一个包含List<string>的单独对象,并将所有数据粘贴在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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