XML列表序列化 [英] XML list serialization
本文介绍了XML列表序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下XML:
<Product>
<Categories>
<Id>1</Id>
<Id>2</Id>
<Id>3</Id>
<Id>4</Id>
<Id>5</Id>
<Id>6</Id>
<Id>7</Id>
<Id>8</Id>
</Categories>
</Product>
我正在使用XmlSerializer
将其反序列化为.NET类型.
我的目标是将其分解为:
I'm deserializing it to the .NET type using XmlSerializer
.
My goal is to have it deserealized as:
public class Product
{
public List<int> Categories { get; set; }
}
不幸的是,我没有设法将它退化为这种类型(Categories
属性为空).有什么想法我该怎么做?
Unfortunately I didn't manage to deserealize it to such type (the Categories
property is empty). Any ideas how can I do it?
我设法做到了,但是我对这种解决方案不满意.
I managed to do it with, but I'm not happy with such solution.
[Serializable]
public class Categories
{
[XmlElement]
public List<Int32> Id { get; set; }
}
public class Product
{
public Categories Categories { get; set; }
}
我知道,我可以使用Linq to XML来做到这一点,但是我想知道是否可以使用XmlSerializer
来做到这一点.
I know, I can do it with Linq to XML, but I wonder if I can do it with XmlSerializer
.
推荐答案
您可以使用XmlArray
和XmlArrayItem
属性:
public class Product
{
[XmlArray]
[XmlArrayItem("Id")]
public List<int> Categories { get; set; }
}
这篇关于XML列表序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文