[英]XML list serialization
I have the following 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>
I'm deserializing it to the .NET type using XmlSerializer
. 我正在使用
XmlSerializer
将其反序列化为.NET类型。 My goal is to have it deserealized as: 我的目标是将其分解为:
public class Product
{
public List<int> Categories { get; set; }
}
Unfortunately I didn't manage to deserealize it to such type (the Categories
property is empty). 不幸的是,我没有设法将它退化为这种类型(
Categories
属性为空)。 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; }
}
I know, I can do it with Linq to XML, but I wonder if I can do it with XmlSerializer
. 我知道,我可以使用Linq to XML来做到这一点,但是我想知道是否可以使用
XmlSerializer
来做到这一点。
You can use the XmlArray
and XmlArrayItem
attributes: 您可以使用
XmlArray
和XmlArrayItem
属性:
public class Product
{
[XmlArray]
[XmlArrayItem("Id")]
public List<int> Categories { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.