I want to reproduce the following XML text using C# classes:
<?xml version="1.0" encoding="UTF-8" ?>
<d id="temp1">
<lr lrp="p_1"><l name="l1"/><l name="l2"/><l name="l3"/><l name="l4"/></lr>
<lr lrp="p_2"><l name="l5"/><l name="l6"/><l name="l7"/></lr>
</d>
I'm using the following classes:
public enum LT
{ NONE, l1 = 0x01, l2 = 0x02, l3 = 0x04, l4 = 0x08, l5 = 0x10, l6 = 0x20, l7 = 0x40 }
public class L
{
public L() { lt = LT.NONE; }
public L(LT l) { lt = l; }
[XmlAttribute("name")]
public LT lt;
}
public class LR: IEnumerable<L>
{
List<L> _elems;
public LR() { _elems = new List<L>(); }
public LR(string lname) { ListName = lname; _elems = new List<L>(); }
[XmlAttribute("lrp")]
public string ListName { get; set; }
[XmlArrayItem("l")]
public L this[int index] { get => _elems[index] ; set => _elems[index] = value; }
public IEnumerator<L> GetEnumerator() { return _elems.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
public void Add(L l) { _elems.Add(l); }
}
[XmlRoot("d")]
public class D : IEnumerable<LR>
{
private List<LR> _elems;
public D() { _elems = new List<LR>(); }
public D(string id) { Name = id; _elems = new List<LR>(); }
[XmlAttribute("id")]
public string Name { get; set; }
[XmlArrayItem("lr")]
public LR this[int index] { get => _elems[index]; set => _elems[index] = value; }
public IEnumerator<LR> GetEnumerator() { return _elems.GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return this.GetEnumerator(); }
public void Add(LR l) { _elems.Add(l); }
}
And using them in a Main like this:
D devices = new D("temp1")
{
{ new LR("p_1") {
{ new L(LT.l1) },
{ new L(LT.l2) },
{ new L(LT.l3) },
{ new L(LT.l4) }
}},
{ new LR("p_2") {
{ new L(LT.l5) },
{ new L(LT.l6) },
{ new L(LT.l7) }
}}
};
XmlSerializer x = new XmlSerializer(devices.GetType());
x.Serialize(Console.Out, devices);
Console.WriteLine();
Console.ReadLine();
But I get the following result:
<?xml version="1.0" encoding="ibm850"?>
<d xmlns:xsi="..." xmlns:xsd="...">
<ArrayOfL>
<L name="l1" />
<L name="l2" />
<L name="l3" />
<L name="l4" />
</ArrayOfL>
<ArrayOfL>
<L name="l5" />
<L name="l6" />
<L name="l7" />
</ArrayOfL>
</devs>
First of all I'm not having of the XML attributes id and lrp defined, and then I have neither lr nor l tags expressed the way I want. How can I possibly solve this without the need of preprocessing XML stream by parts?