0

I have some classes and I need to obtain this xml by serialization:

<?xml version="1.0" encoding="utf-8"?>
<AAA attr1="10" attr2="250" >  
  <params>
    <rows>
      <row>
        <field_1>123456</field_1>
        <field_2>999</field_2>        
      </row>
    </rows>
  </params>
</AAA>

These are the classes:

public class Row
    {
        [XmlAttribute("field_1")]
        public String Field1
        {
            get;
            set;
        }

        [XmlAttribute("field_2")]
        public int Field2
        {
            get;
            set;
        }
    }

public class Parameters
    {
        [XmlArray("rows")]
        [XmlArrayItem("row")]
        public List<Row> rows = new List<Row>();
    }



[XmlRoot(ElementName = "AAA")]
public class Base
    {
        [XmlAttribute("attr1")]
        public String Attribute1 = "6687";

        [XmlAttribute("attr2")]
        public String Attribute2 = "65";

        [XmlArray("params")]
        [XmlArrayItem("rows")]
        public List<Parameters> parameters = new List<Parameters>();
    }

I need some help in setting xml attributes to the classes so when I'm serializing, to obtain the above xml.

Regards

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • It seems you only need to replace the XmlAttribute on the properties of the Row class with XmlElement so you get an element instead of an attribute. What problems are you having? – Angelo Reis Apr 07 '15 at 08:57
  • I've replaced that but I am obtaining two rows tags ( ) – Buda Gavril Apr 07 '15 at 09:25

1 Answers1

0

I've fixed it, here are the classes:

public class Row
    {
        [XmlElement("field_1")]
        public String Field1
        {
            get;
            set;
        }

        [XmlElement("field_2")]
        public int Field2
        {
            get;
            set;
        }
    }

public class Parameters
    {
        [XmlArray("rows")]
        [XmlArrayItem("row")]
        public List<Row> rows = new List<Row>();
    }



[XmlRoot(ElementName = "AAA")]
public class Base
    {
        [XmlAttribute("attr1")]
        public String Attribute1 = "6687";

        [XmlAttribute("attr2")]
        public String Attribute2 = "65";

        [XmlElement("params")]
        public List<Parameters> parameters = new List<Parameters>();
    }
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196