本文介绍了如何使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>
我想删除 Items容器标签来生成它:
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.
推荐答案
在您的属性:
Specify an element name in your [XmlElement]
attribute:
[XmlElement("MyDatum", Type = typeof(MyDatum))]
public List<MyDatum> Items {
// ...
}
根据本文在MSDN上,这将删除序列化项目周围的wrapper元素。
According to this article on MSDN, this will remove the wrapper element around serialized items.
这篇关于如何使XmlSerializer不序列化列表容器标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!