What I suggest you do is to download the Nuget package ExtendedXmlSerializer. Instantiate a List<XmlData>
and Serialize it using the Serializer, then examine the output.
List<XmlData> parents = new List<XmlData>
{
new XmlData{child1="A1",child2="A2",child3="A3"},
new XmlData{child1="B1",child2="B2",child3="B3"},
new XmlData{child1="C1",child2="C2",child3="C3"}
};
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
var xml = serializer.Serialize(parents);
The output Xml will be similar to this.
<?xml version="1.0" encoding="utf-8"?><List xmlns:ns1="clr-namespace:XmlTest;
assembly=XmlTest" xmlns:exs="https://extendedxmlserializer.github.io/v2" exs:arguments="ns1:XmlData" xmlns="https://extendedxmlserializer.github.io/system">
<Capacity>4</Capacity>
<ns1:XmlData>
<child1>A1</child1>
<child2>A2</child2>
<child3>A3</child3>
</ns1:XmlData>
<ns1:XmlData>
<child1>B1</child1>
<child2>B2</child2>
<child3>B3</child3>
</ns1:XmlData>
<ns1:XmlData>
<child1>C1</child1>
<child2>C2</child2>
<child3>C3</child3>
</ns1:XmlData></List>
Next you want to restructure your xml data so that it's in the same format. The
Capacity
element can be omitted. If the data file format you have is fixed, you can restructure it by loading it into a
StringBuilder
and employing the
Append
and
Replace
methods to add and replace element names as required.
string xmlContent = File.ReadAllText(@"C:\Temp\orig.xml");
StringBuilder sb = new StringBuilder(xmlContent).
Replace("<Parents>",xmlHeading).Replace("</Parents>","").
Replace("parent","ns1:XmlData").Append("/List>");
IExtendedXmlSerializer serializer = new ConfigurationContainer().Create();
string data = sb.ToString();
var MyXmlDataList = serializer.Deserialize<List<XmlData>>(data);