我正在尝试将类序列化为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;
}
}
}
}
答案 0 :(得分: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;
}
}