I’m trying to write XML files.
I’ve got a list of Configuration.
[Serializable]
public class Configuration
{
public decimal X { get; set; }
public decimal Y { get; set; }
}
And this is my property.
[XmlArray("Configurations")]
[XmlArrayItem("Configuration")]
public List<Configuration> Configurations { get;set; }
The caller is:
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();
And the output is:
<?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>
There’s a way that can generate this XML file?
<?xml version="1.0" encoding="utf-8"?>
<Configurations>
<Configuration X="1" Y="10" />
<Configuration X="10" Y="100" />
</Configurations>
Is there a way to generate them in this way?
Try to use following attributes: