[英].NET How to serialize a TimeSpan to XML (not the same question!)
[英]How to serialize this Xml in .NET (array)
我需要看起來像這樣的Xml
<foo>
<bar ... />
<bar ... />
</foo>
目前有以下類結構:
[XmlRoot("foo")]
public class Foo
{
[XmlArrayItem("bar")]
public List<Bar> myBars;
}
但這給了我Xml,其中條形項包含在一個條形元素中。 我應該如何定義我的自定義XmlAttributes,以便獲得我需要的Xml結構?
我昨天必須解決類似問題,這對我來說是解決方案:
[XmlRoot("foo")]
public class Foo
{
[XmlElement("bar")]
public List<Bar> myBars;
}
我使用的解決方案是這樣的:
[XmlRoot("foo")]
public class Foo : List<Bar>
{
}
[XmlType("bar")]
public class Bar
{
}
事實上,我將Foo
定義為List<T>
,因此它可以作為通用列表。 該列表中的類型只需要定義XmlType
屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.