C# 如何设置xml类属性以获取请求的xml?
我有一些类,我需要通过序列化获得此xml:C# 如何设置xml类属性以获取请求的xml?,c#,xml-serialization,xml-attribute,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>
<?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>
123456
999
这些是课程:
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>();
}
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>();
}
公共类行
{
[XmlAttribute(“字段_1”)]
公共字符串字段1
{
得到;
设置
}
[XmlAttribute(“字段_2”)]
公共int字段2
{
得到;
设置
}
}
公共类参数
{
[XmlArray(“行”)]
[XmlArrayItem(“行”)]
公共列表行=新列表();
}
[XmlRoot(ElementName=“AAA”)]
公共阶级基础
{
[XmlAttribute(“attr1”)]
公共字符串Attribute1=“6687”;
[xmldattribute(“attr2”)]
公共字符串Attribute2=“65”;
[XmlArray(“params”)]
[XmlArrayItem(“行”)]
公共列表参数=新列表();
}
我需要一些帮助来设置类的xml属性,以便在序列化时获得上述xml
关于我已经修好了,以下是课程:
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>();
}
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>();
}
公共类行
{
[XmlElement(“字段1”)]
公共字符串字段1
{
得到;
设置
}
[XmlElement(“字段_2”)]
公共int字段2
{
得到;
设置
}
}
公共类参数
{
[XmlArray(“行”)]
[XmlArrayItem(“行”)]
公共列表行=新列表();
}
[XmlRoot(ElementName=“AAA”)]
公共阶级基础
{
[XmlAttribute(“attr1”)]
公共字符串Attribute1=“6687”;
[xmldattribute(“attr2”)]
公共字符串Attribute2=“65”;
[XmlElement(“参数”)]
公共列表参数=新列表();
}
Row类的属性上的XmlAttribute似乎只需替换为XmlElement,就可以得到一个元素而不是属性。你有什么问题?我已经替换了它,但是我得到了两行标签()