Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在两个不同的元素中反序列化具有相同属性名称的XML?_C#_Xml_Xml Deserialization - Fatal编程技术网

C# 如何在两个不同的元素中反序列化具有相同属性名称的XML?

C# 如何在两个不同的元素中反序列化具有相同属性名称的XML?,c#,xml,xml-deserialization,C#,Xml,Xml Deserialization,我的班级定义: [Serializable] public class MyClass { [XmlAttribute(AttributeName = "ID")] //Problem is here. same attr name ID. public int XXX_ID { get; set; } [XmlElement(ElementName = "XXX")] public string XXX_Value{ get; set; } [Xml

我的班级定义:

[Serializable]
public class MyClass
{
    [XmlAttribute(AttributeName = "ID")] //Problem is here. same attr name ID.
    public int XXX_ID { get; set; }

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

    [XmlAttribute(AttributeName = "ID")] //Problem is here. same attr name ID.
    public int YYY_ID { get; set; }

    [XmlElement(ElementName = "YYY")]
    public string YYY_Value { get; set; }
}
我的XML:

<MyClass>
    <XXX ID="123">Some Values</XXX> 
    <YYY ID="567">Some Values</YYY>
</MyClass>

一些价值观
一些价值观
我的问题是:

我想将上述XML反序列化为一个对象

在运行时发生错误,不允许在两个不同的元素和同一根下具有相同的属性名称

如何解决这个问题

p/S:我不能更改XML,我不是它的所有者


提前感谢。

要做到这一点,您需要手动执行(反)序列化,或者需要使用与xml大致相同的布局。例如:

public class Something { // need a name here to represent what this is!
    [XmlAttribute] public int ID {get;set;}
    [XmlText] public string Value {get;set;}
}
然后


注意:
[Serializable]
在这里没有任何用处
public class MyClass {
    public Something XXX {get;set;}
    public Something YYY {get;set;}
}