[Solved] How to convert class Array to XML file in C# dynamically?


Instead of manually constructing the XML file, use a XML serializer instance.

For it to correctly generate the structure, use a wrapper-class with decorated properties as follows:

class XmlOrderTemplate {
  [XmlArray("OrderTemplate")]
  [XmlArrayItem("Order")]
  public List<OrderTemplate> Orders {get;set;}
}

using(var sw = new StreamWriter(fullPath)){
  var serializer = new XmlSerializer(typeof(XmlOrderTemplate));
   serializer.Serialize(sw, new XmlOrderTemplate {Orders = Data});
}

2

solved How to convert class Array to XML file in C# dynamically?