Reputation: 13649
I'm trying to serialize a class into an XML file that should look like this:
<Configuration>
<LookupTables>
<Languages>
<Language>
<Name>Dutch</Name>
</Language>
<Language>
<Name>French</Name>
</Language>
</Languages>
</LookupTables>
</Configuration>
But instead I get this output:
<Configuration>
<LookupTables>
<Languages>
<Name>Dutch</Name>
</Languages>
<Languages>
<Name>French</Name>
</Languages>
</LookupTables>
</Configuration>
Is there anything wrong with my code?
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;
}
}
}
}
Upvotes: 2
Views: 40
Reputation: 123
I think the problem is to declare an Array as XmlElement
, use the XmlArray
Attributes instead.
[System.Xml.Serialization.XmlArrayAttribute("Languages")]
[System.Xml.Serialization.XmlArrayItemAttribute("Language")]
public Language_Type[] Languages
{
get
{
return this.languagesField;
}
set
{
this.languagesField = value;
}
}
Upvotes: 2