关于.net中的XML反序列化的问题(Question about XML deserialization in .net)

编程入门 行业动态 更新时间:2024-10-14 18:17:29
关于.net中的XML反序列化的问题(Question about XML deserialization in .net)

我正在尝试反序列化来自Web服务的XML,但我不知道如何告诉序列化程序如何处理这段xml:

<Movimientos> <Movimientos> <NOM_ASOC>pI22E7P30KWB9KeUnI+JlMRBr7biS0JOJKo1JLJCy2ucI7n3MTFWkY5DhHyoPrWs</NOM_ASOC> <FEC1>RZq60KwjWAYPG269X4r9lRZrjbQo8eRqIOmE8qa5p/0=</FEC1> <IDENT_CLIE>IYbofEiD+wOCJ+ujYTUxgsWJTnGfVU+jcQyhzgQralM=</IDENT_CLIE> </Movimientos> <Movimientos>

正如您所看到的,子标记使用与其父标记相同的标记,我认为这是错误的,但是Web服务是由外部公司提供的并且不会更改它,是否有任何方式或任何库来整理XML或者如何我在我的类上使用了一个属性,以便序列化器能够正确使用它? 谢谢你的帮助。

I'm trying to deserialize an XML that comes from a web service but I do not know how to tell the serializer how to handlke this piece of xml:

<Movimientos> <Movimientos> <NOM_ASOC>pI22E7P30KWB9KeUnI+JlMRBr7biS0JOJKo1JLJCy2ucI7n3MTFWkY5DhHyoPrWs</NOM_ASOC> <FEC1>RZq60KwjWAYPG269X4r9lRZrjbQo8eRqIOmE8qa5p/0=</FEC1> <IDENT_CLIE>IYbofEiD+wOCJ+ujYTUxgsWJTnGfVU+jcQyhzgQralM=</IDENT_CLIE> </Movimientos> <Movimientos>

As you can see, the child tag uses the same tag as its parent, I suppose this is wrong however the webservice is provided by an external company and that wont change it, is there any way or any library to tidy up XML or how can I use an attribute on my class so that the serializer gets it right? thanks for any help.

最满意答案

序列化程序应该没问题 - 问题可能只是一个类不能拥有与类本身同名的属性。 因此,使用XmlElementAttribute来匹配它:

[XmlRoot("Movimientos")] public class Movimientos { [XmlElement("Movimientos")] public SomeOtherClass SomeOtherProperty { get; set; } } public class SomeOtherClass { public string NOM_ASOC { get; set; } public string FEC1 { get; set; } public string IDENT_CLIE { get; set; } }

仅供参考,XML实际上并没有错; 规范中没有规则说子元素不能与其父元素具有相同的名称。 这可能是不寻常的,但仍然有效。

The serializer should be OK with it - the issue is probably just that a class can't have a property that's the same name as the class itself. So use XmlElementAttribute to match it up:

[XmlRoot("Movimientos")] public class Movimientos { [XmlElement("Movimientos")] public SomeOtherClass SomeOtherProperty { get; set; } } public class SomeOtherClass { public string NOM_ASOC { get; set; } public string FEC1 { get; set; } public string IDENT_CLIE { get; set; } }

FYI, the XML is actually not wrong; there's no rule in the spec saying that a child element can't have the same name as its parent element. It's unusual maybe, but still valid.

更多推荐

本文发布于:2023-08-07 06:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462873.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列化   XML   net   Question   deserialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!