我有一个xml
<child>
<parameter>
<displayName >animal</displayName>
<type>terrestrial</type>
</parameter>
<subchild >
<parameter>
<displayName >horse</displayName>
<type>terrestrial</type>
</parameter>
</subchild>
</child>
,它表示为类
公共部分班级儿童{
private childSubchild[] itemsField;
[System.Xml.Serialization.XmlElementAttribute("subchild", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public childSubchild[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
public partial class childSubchild {
private childSubchildParameter[] parameterField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("parameter", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public childSubchildParameter[] parameter {
get {
return this.parameterField;
}
set {
this.parameterField = value;
}
}
}
public partial class childSubchildParameter {
private string displayNameField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string displayName {
get {
return this.displayNameField;
}
set {
this.displayNameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string type {
get {
return this.typeField;
}
set {
this.typeField = value;
}
}
}
我需要在序列化数据时添加更多子项
答案 0 :(得分:0)
试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(Child));
XmlTextReader reader = new XmlTextReader(FILENAME);
Child child = (Child)xs.Deserialize(reader);
}
}
[XmlRoot("child")]
public class Child
{
[XmlElement("parameter")]
public ChildSubchildParameter[] parameter {get; set; }
[XmlElement("subchild")]
public SubChild[] subchild {get; set;}
}
[XmlRoot("subchild")]
public class SubChild
{
[XmlElement("parameter")]
public ChildSubchildParameter[] parameter {get; set; }
}
[XmlRoot("parameter")]
public class ChildSubchildParameter
{
[XmlElement("displayName")]
public string displayName {get; set; }
[XmlElement("type")]
public string type {get; set; }
}
}