There are 2 ways at least:
1)
serialization and deserialization[
^]
A Complete Sample of Custom Class Collection Serialization and Deserialization[
^]
and
2)
LinqToXML[
^]
Example (using
LinqPad[
^]):
void Main()
{
List<ExecuteProcessTemplate> epts = new List<ExecuteProcessTemplate>{
new ExecuteProcessTemplate(123456, 789456,
new List<Input>{new Input(1, "A", "B"),new Input(2, "A", "C"),
new Input(3, "A", "D"),new Input(4, "A", "E")}),
new ExecuteProcessTemplate(123457, 789457,
new List<Input>{new Input(1, "B", "C"),new Input(2, "B", "D"),
new Input(3, "B", "E"),new Input(4, "B", "F")})
};
XElement EptsToXmls =
new XElement("ExecuteProcessTemplates",
from ept in epts select new XElement("ExecuteProcessTemplate",
new XElement("ProjectID", ept.ProjectID),
new XElement("ProcessTemplateID", ept.ProcessTemplateID),
new XElement("Inputs",
from inp in ept.Inputs select
new XElement("Input",
new XElement("Id", inp.Id),
new XElement("Type", inp.Type),
new XElement("Value", inp.Value)
))));
EptsToXmls.Dump();
EptsToXmls.Save(@"D:\epts.xml");
}
public class ExecuteProcessTemplate
{
private long pid =0;
private long ptid =0;
private List<Input> lst = new List<Input>();
public ExecuteProcessTemplate(long _pid, long _ptid, List<Input> _lst)
{
pid = _pid;
ptid = _ptid;
lst = _lst;
}
public long ProjectID
{
get{return pid;}
set{pid = value;}
}
public long ProcessTemplateID
{
get{return ptid;}
set{ptid = value;}
}
public List<Input> Inputs
{
get{return lst;}
set{lst = value;}
}
}
public class Input
{
private int id = 0;
private string typ = string.Empty;
private string val = string.Empty;
public Input(int _id, string _typ, string _val)
{
id = _id;
typ = _typ;
val = _val;
}
public int Id
{
get{return id;}
set{id = value;}
}
public string Type
{
get{return typ;}
set{typ = value;}
}
public string Value
{
get{return val;}
set{val = value;}
}
}
Result:
<ExecuteProcessTemplates>
<ExecuteProcessTemplate>
<ProjectID>123456</ProjectID>
<ProcessTemplateID>789456</ProcessTemplateID>
<Inputs>
<Input>
<Id>1</Id>
<Type>A</Type>
<Value>B</Value>
</Input>
<Input>
<Id>2</Id>
<Type>A</Type>
<Value>C</Value>
</Input>
<Input>
<Id>3</Id>
<Type>A</Type>
<Value>D</Value>
</Input>
<Input>
<Id>4</Id>
<Type>A</Type>
<Value>E</Value>
</Input>
</Inputs>
</ExecuteProcessTemplate>
<ExecuteProcessTemplate>
<ProjectID>123457</ProjectID>
<ProcessTemplateID>789457</ProcessTemplateID>
<Inputs>
<Input>
<Id>1</Id>
<Type>B</Type>
<Value>C</Value>
</Input>
<Input>
<Id>2</Id>
<Type>B</Type>
<Value>D</Value>
</Input>
<Input>
<Id>3</Id>
<Type>B</Type>
<Value>E</Value>
</Input>
<Input>
<Id>4</Id>
<Type>B</Type>
<Value>F</Value>
</Input>
</Inputs>
</ExecuteProcessTemplate>
</ExecuteProcessTemplates>