gpt4 book ai didi

c# - 编写 XML 文件?

转载 作者:行者123 更新时间:2023-11-30 21:08:52 25 4
gpt4 key购买 nike

我正在尝试编写 XML 文件。

我有一个配置列表。

[Serializable]
public class Configuration
{
public decimal X { get; set; }
public decimal Y { get; set; }
}

这是我的属性(property)。

    [XmlArray("Configurations")]
[XmlArrayItem("Configuration")]
public List<Configuration> Configurations { get;set; }

来电者是:

    this.Configurations = new List<Configuration>()
{
new Configuration() { X = 1, Y = 10 },
new Configuration() { X = 10, Y = 100 },
};

string filename = "test.xml";

TextWriter writer = new StreamWriter(filename);
XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>));
serializer.Serialize(writer, this.Configurations));
writer.Close();

输出是:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Configuration x="1" y="10" />
<Configuration x="10" y="100" />
</ArrayOfConfiguration>

有没有一种方法可以生成这个 XML 文件?

<?xml version="1.0" encoding="utf-8"?> 
<Configurations>
<Configuration X="1" Y="10" />
<Configuration X="10" Y="100" />
</Configurations>

有没有办法以这种方式生成它们?

最佳答案

尝试使用以下属性:

[XmlArray("Configurations")]
[XmlArrayItem("Configuration")]

关于c# - 编写 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9336555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com