[英].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.