xmlserializer 反序列化包含属性的列表
我有 xml,结构的一部分如下所示:
<IDList>
<ValuesList ID="1">
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
</ValuesList>
<ValuesList ID="2">
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
</ValuesList>
</IDList>
xmlserializer 的模型类应该是什么,这样我就可以正确地反序列化它? 在 IDList 级别上,这非常简单:
[XmlArray("IDList")]
[XmlArrayItem("ValuesList")]
public List<CValuesList> idList = new List<CValuesList>();
但是当数组元素具有额外的 xml 属性时我该怎么办?
我尝试按照此处所示的方式执行操作:
http://www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html
但它对我不起作用。数组的元素尚未反序列化。
I have xml that part of structure looks like this:
<IDList>
<ValuesList ID="1">
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
</ValuesList>
<ValuesList ID="2">
<Value>1</Value>
<Value>2</Value>
<Value>3</Value>
</ValuesList>
</IDList>
What should be model classes for xmlserializer, so I could deserialize it properly?
On the level of IDList it's quite easy:
[XmlArray("IDList")]
[XmlArrayItem("ValuesList")]
public List<CValuesList> idList = new List<CValuesList>();
but how can I do it when the array element has extra xml attribute?
I tried to do ot in the way shown here:
http://www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html
but it didn't worked for me. The elements of the array haven't been deserialized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有简单的方法可以做到这一点。
一个好的解决方案是创建一个包含列表和属性的对象,并继承/实现 IXmlSerialized。
我在这个网站上看到了某种快速而肮脏的方法:http://funcakes.posterous.com/adding-elements-to-lists-in-the-xmlserializer。由于该对象不是从 ISerialized 继承的,因此序列化程序不会将其序列化为列表。
There is no simple way to do that.
A good solution would be to create an object that contains a list and your properties and inherit/implements IXmlSerializable.
I've seen some kind of quick and dirty way on this site: http://funcakes.posterous.com/adding-elements-to-lists-in-the-xmlserializer. Since the object doesn't inherit from ISerializable, it won't be serialized as a list by the serializer.