我如何获得的XmlSerializer不序列化容器标签? [英] How do I get XmlSerializer to not serialize container tags?
本文介绍了我如何获得的XmlSerializer不序列化容器标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的对象图,我想序列化,我一直没能找到解决这个问题。在这里,它是:
I have a simple object graph that I would like to serialize, I haven't been able to find a solution to this problem. Here it is:
[XmlRoot]
public partial class MyData
{
private List<MyDatum> itemsField;
public MyData()
{
this.anyAttrField = new List<System.Xml.XmlAttribute>();
this.itemsField = new List<MyDatum>();
}
[XmlElement(Type = typeof(MyDatum))]
public List<MyDatum> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
这会产生以下XML:
This produces the following XML:
<MyData>
<Items>
<MyDatum/>
<MyDatum/>
...
</items>
</MyData>
我想删除项目集装箱电子标签生产这个代替:
I would like to remove the "Items" container tag to produce this instead:
<MyData>
<MyDatum/>
<MyDatum/>
...
</MyData>
我已经试过各种解决方案,但似乎无法找到解决的办法。
I've tried all kinds of solutions, but can't seem to find a solution.
推荐答案
在您的 [的XmlElement]
属性:
Specify an element name in your [XmlElement]
attribute:
[XmlElement("MyDatum", Type = typeof(MyDatum))]
public List<MyDatum> Items {
// ...
}
据的本文在MSDN上,这将消除围绕系列化项目包装元素。
According to this article on MSDN, this will remove the wrapper element around serialized items.
这篇关于我如何获得的XmlSerializer不序列化容器标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文