如何使XmlSerializer不序列化列表容器标签?

编程入门 行业动态 更新时间:2024-10-11 01:17:24
本文介绍了如何使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.

推荐答案

在您的 [XmlElement] 属性:

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不序列化列表容器标签?

本文发布于:2023-11-06 03:30:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1562614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   标签   序列化   列表   XmlSerializer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!