如何让 XmlSerializer 不序列化列表容器标记?

发布于 2024-12-17 07:37:53 字数 979 浏览 2 评论 0原文

我有一个简单的对象图,我想序列化,但我一直无法找到这个问题的解决方案。它是这样的:

   [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-12-24 07:37:53

[XmlElement]属性:

[XmlElement("MyDatum", Type = typeof(MyDatum))]
public List<MyDatum> Items {
    // ...
}

根据本文 在 MSDN 上,这将删除序列化项周围的包装元素。

Specify an element name in your [XmlElement] attribute:

[XmlElement("MyDatum", Type = typeof(MyDatum))]
public List<MyDatum> Items {
    // ...
}

According to this article on MSDN, this will remove the wrapper element around serialized items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文