[英]XML Serializing a List<Class>
我在反序列化XML時遇到問題,我不太清楚。 主要是,我試圖避免在類位於列表內但未成功時嵌套類。 例如:
[XmlRoot]
[Serializable]
public class Foo
{
[XmlElement("Bar")]
public BarElement Bar = new BarElement();
public class BarElement
{
[XmlElement("MoreBars")]
public List<MoreElement> MoreBars = new List<MoreElement>();
}
[XmlRoot("More")]
[Serializable]
public class MoreElement
{
[XmlAttribute("Attribute")]
public string Attribute { get; set; }
[XmlText]
public string Value { get; set; }
}
}
對應於:
<Foo>
<Bar>
<MoreBars>
<More Attribute=""></More>
<More Attribute=""></More>
<More Attribute=""></More>
<More Attribute=""></More>
</MoreBars>
</Bar>
</Foo>
這幾乎可以工作...但效果不佳。 通過將XmlRoot添加到MoreElement,我試圖避免必須創建一個僅包含MoreElement列表的名為“ MoreBarsElement”的新類的麻煩,因為訪問“ Foo.Bar.MoreBars.Value”已經很麻煩了。 。 這可能嗎? 如果是這樣,我該怎么辦?
回答我自己的問題-看起來我需要
[XmlArray("MoreBars"), XmlArrayItem(typeof(MoreElement), ElementName = "More")]
關於數組項的聲明。 現在就可以了,萬歲!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.