0

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?

delverdl
  • 85
  • 10
  • It is a documented restriction that `XmlSerializer` does not serialize properties of collections. See [When a class is inherited from List<>, XmlSerializer doesn't serialize other attributes](https://stackoverflow.com/q/5069099/3744182). You will need to modify your data model to use some intermediate container class for the attributes, see e.g. [XML serialization of a list with attributes](https://stackoverflow.com/q/13811991/3744182). In fact I think your question is a duplicate of those two, agree? – dbc May 24 '22 at 16:12
  • 1
    Upload your XML to https://xmltocsharp.azurewebsites.net/ and you will get a data model that works. See [this answer](https://stackoverflow.com/a/37839900/3744182) to [Generate C# class from XML](https://stackoverflow.com/q/4203540/3744182). Of note, applying `[XmlElement]` to a collection property causes the collection to be serialized without an outer wrapper element. – dbc May 24 '22 at 16:15
  • Thanks a lot @dbc. This site works fine and I could give your last comment as the right answer. – delverdl May 24 '22 at 16:50

0 Answers0