如何反序列化 2 个不同元素中具有相同属性名称的 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 反序列化为一个对象。
运行时出现错误,同一根下的2个不同元素中不允许有相同的属性名称。
如何解决这个问题呢?
P/S:我无法更改 XML,我不是它的所有者。
提前致谢。
My class definition:
[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; }
}
My XML:
<MyClass>
<XXX ID="123">Some Values</XXX>
<YYY ID="567">Some Values</YYY>
</MyClass>
My Problem:
I want to de-serialize the above XML into an object.
During the runtime, an error has occurred, it is not allowed to have same attribute name in 2 different elements and under the same root.
How to solve this problem?
P/S: I cannot change the XML, I am not the owner of it.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您要么需要手动执行(反)序列化,要么需要 DTO 具有与 xml 大致相同的布局。例如:
那么
To do that you either need to do the (de)serialization manually, or you need to DTO to have roughly the same layout as the xml. For example:
then