2017-03-27 33 views
2

我试图序列化类到XML文件看起来应该像这样的:类不能被序列化到所需的格式

<Configuration> 
    <LookupTables> 
    <Languages> 
     <Language> 
     <Name>Dutch</Name> 
     </Language> 
     <Language> 
     <Name>French</Name> 
     </Language> 
    </Languages> 
    </LookupTables> 
</Configuration> 

而是我得到这个输出:

<Configuration> 
    <LookupTables> 
    <Languages> 
     <Name>Dutch</Name> 
    </Languages> 
    <Languages> 
     <Name>French</Name> 
    </Languages> 
    </LookupTables> 
</Configuration> 

我的代码有什么问题吗?

namespace MyProject 
{ 
    /// <remarks/> 
    [System.SerializableAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "")] 
    [System.Xml.Serialization.XmlRootAttribute(ElementName="Configuration", Namespace = "", IsNullable = false)] 
    public class Configuration_Type 
    { 
     private LookupTables_Type lookupTablesField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("LookupTables")] 
     public LookupTables_Type LookupTables 
     { 
      get 
      { 
       return this.lookupTablesField; 
      } 
      set 
      { 
       this.lookupTablesField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.SerializableAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
    [System.Xml.Serialization.XmlRootAttribute(ElementName="LookupTables", Namespace = "", IsNullable = false)] 
    public class LookupTables_Type 
    { 

     private Language_Type[] languagesField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute("Languages")] 
     public Language_Type[] Languages 
     { 
      get 
      { 
       return this.languagesField; 
      } 
      set 
      { 
       this.languagesField = value; 
      } 
     } 
    } 

    /// <remarks/> 
    [System.SerializableAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
    [System.Xml.Serialization.XmlRootAttribute(ElementName="Language", Namespace = "", IsNullable = false)] 
    public class Language_Type 
    { 

     private string nameField; 

     /// <remarks/> 
     [System.Xml.Serialization.XmlElementAttribute()] 
     public string Name 
     { 
      get 
      { 
       return this.nameField; 
      } 
      set 
      { 
       this.nameField = value; 
      } 
     } 
    } 
} 

回答

2

我认为这个问题是声明数组作为XmlElement,使用XmlArray属性来代替。

[System.Xml.Serialization.XmlArrayAttribute("Languages")] 
[System.Xml.Serialization.XmlArrayItemAttribute("Language")] 
public Language_Type[] Languages 
{ 
    get 
    { 
     return this.languagesField; 
    } 
    set 
    { 
     this.languagesField = value; 
    } 
} 
+0

你是对的..它的工作:) – yonan2236