我正在尝试编写XML文件。
我有一个配置清单。
[Serializable]
public class Configuration
{
public decimal X { get; set; }
public decimal Y { get; set; }
}
这是我的财产。
[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>
有没有办法以这种方式生成它们?
答案 0 :(得分:4)
尝试使用以下属性:
[XmlArray("Configurations")]
[XmlArrayItem("Configuration")]