有/没有 XmlElement 的 XmlSerializer 中的不同行为

发布于 2024-12-23 18:44:35 字数 1977 浏览 1 评论 0原文

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Linq;

namespace Serialize
{    
    public class Good
    {
        public int a;
        public Good() {}

        public Good(int x)
        {
            a = x;
        }
    }

    public class Hello
    {
        public int x;
        public List<Good> goods = new List<Good>();

        public Hello()
        {
            goods.Add(new Good(1));
            goods.Add(new Good(2));
        }
    }

    [XmlRootAttribute("Component", IsNullable = false)]
    public class Component {
        //[XmlElement("worlds_wola", IsNullable = false)]
        public List<Hello> worlds;      

        public Component()
        {
            worlds = new List<Hello>() {new Hello(), new Hello()}; 
        }
    }

    class Cov2xml
    {
        static void Main(string[] args)
        {
            string xmlFileName = "ip-xact.xml";
            Component comp = new Component();

            TextWriter writeFileStream = new StreamWriter(xmlFileName);

            var ser = new XmlSerializer(typeof(Component));
            ser.Serialize(writeFileStream, comp);
            writeFileStream.Close();

        }
    }
}

通过此 XmlSerializer 代码,我获得了此 XML 文件。

在此处输入图像描述

我只有一个“worlds”元素,其中有两个 Hello 元素。

但是,当我在世界变量之前添加 XmlElement 时。

[XmlElement("worlds_wola", IsNullable = false)]
public List<Hello> worlds

我有两个 worlds_wola 元素而不是一个。

在此处输入图像描述

这是为什么?如何使用 XmlElement 指定标签的名称,但只有一个“worlds_wola”元素,如下所示?

<worlds_wola>
  <Hello>
   ...
  </Hello>
  <Hello>
   ...
  </Hello>
</worlds_wola>
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using System.Linq;

namespace Serialize
{    
    public class Good
    {
        public int a;
        public Good() {}

        public Good(int x)
        {
            a = x;
        }
    }

    public class Hello
    {
        public int x;
        public List<Good> goods = new List<Good>();

        public Hello()
        {
            goods.Add(new Good(1));
            goods.Add(new Good(2));
        }
    }

    [XmlRootAttribute("Component", IsNullable = false)]
    public class Component {
        //[XmlElement("worlds_wola", IsNullable = false)]
        public List<Hello> worlds;      

        public Component()
        {
            worlds = new List<Hello>() {new Hello(), new Hello()}; 
        }
    }

    class Cov2xml
    {
        static void Main(string[] args)
        {
            string xmlFileName = "ip-xact.xml";
            Component comp = new Component();

            TextWriter writeFileStream = new StreamWriter(xmlFileName);

            var ser = new XmlSerializer(typeof(Component));
            ser.Serialize(writeFileStream, comp);
            writeFileStream.Close();

        }
    }
}

With this XmlSerializer code, I get this XML file.

enter image description here

I have only one "worlds" element, that has two Hello elements.

However, when I add XmlElement before the worlds varibale.

[XmlElement("worlds_wola", IsNullable = false)]
public List<Hello> worlds

I have two worlds_wola elements instead of one.

enter image description here

Why is this? How can I use XmlElement to specify the name of the tag, but with only one "worlds_wola" element as follows?

<worlds_wola>
  <Hello>
   ...
  </Hello>
  <Hello>
   ...
  </Hello>
</worlds_wola>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

木槿暧夏七纪年 2024-12-30 18:44:35

您需要对集合使用 XmlArrayAttribute 而不是 XmlElementAttribute。

You need to use the XmlArrayAttribute for your collection instead of the XmlElementAttribute.

装纯掩盖桑 2024-12-30 18:44:35

根据Charles的回答,我发现这正是我想要的。

[XmlArray("fileSet")]
[XmlArrayItem(ElementName = "file", IsNullable = false)]
public List<Hello> worlds;   

通过这个设置,我可以得到

<fileSet>
    <file>...</file>

而不是

<worlds>
    <Hello>...</Hello>

I found that this is exactly what I wanted based on Charles' answer.

[XmlArray("fileSet")]
[XmlArrayItem(ElementName = "file", IsNullable = false)]
public List<Hello> worlds;   

With this setup, I could get

<fileSet>
    <file>...</file>

Instead of

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