[英].NET XML Serializer - Array Custom Attributes
我對如何實現這樣的XML結構感到非常困惑:
<sizes type=”1”>
<size status=”1”>L</size>
<size status=”1”>XL</size>
<size status=”0”>XXL</size>
<sizes>
我能夠創建這樣的結構:
<sizes>
<size>L</size>
<size>XL</size>
<size>XXL</size>
<sizes>
具有XmlArray
和XmlArrayItem
屬性。
[XmlArray(ElementName = "sizes")]
[XmlArrayItem(ElementName = "size")]
但是我無法做的是添加這些自定義屬性。 我該怎么做? 我是否需要創建一個新對象來保存這些值並為其設置自定義屬性?
您應該將它們定義為屬性。 這應該工作:
using System.Collections.Generic;
using System.Xml.Serialization;
public class sizes
{
[XmlAttribute("type")]
public string type { get; set; }
[XmlElement("size")]
public List<size> sizeList { get; set; }
}
public class size
{
[XmlAttribute("status")]
public string status { get; set; }
}
以及反序列化的代碼:
string xml = File.ReadAllText("XMLFile1.xml");
XmlSerializer ser = new XmlSerializer(typeof(sizes));
var sizes = ser.Deserialize(new StringReader(xml));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.