如何设置xml类属性以获取请求的xml?

时间:2015-04-07 08:27:41

标签: c# xml-serialization xml-attribute

我有一些类,我需要通过序列化来获取这个xml:

<?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>

这些是类:

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>();
    }

我需要一些帮助来设置类的xml属性,所以当我序列化时,要获得上面的xml。

此致

1 个答案:

答案 0 :(得分:0)

我已经修好了,这是课程:

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>();
    }