如何让 XmlSerializer 不序列化列表容器标记?
我有一个简单的对象图,我想序列化,但我一直无法找到这个问题的解决方案。它是这样的:
[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:
<MyData>
<Items>
<MyDatum/>
<MyDatum/>
...
</items>
</MyData>
我想删除“Items”容器标签来产生这个:
<MyData>
<MyDatum/>
<MyDatum/>
...
</MyData>
我已经尝试了各种解决方案,但似乎找不到解决方案。
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;
}
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
[XmlElement]
属性:根据本文 在 MSDN 上,这将删除序列化项周围的包装元素。
Specify an element name in your
[XmlElement]
attribute:According to this article on MSDN, this will remove the wrapper element around serialized items.