2015-05-22 5 views
0

У меня есть XMLКак добавить больше данных при сериализации в 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; 
    } 
} 

}

общественный неполный класс 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; 
    } 
} 

}

общественный частичный класс 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; 
    } 
} 

}

мне нужно добавить больше subchild при сериализации данных

ответ

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; } 
    } 
} 
​