I'm trying to create a class that can be serialized into XML via the XMLSerializer.
Destination XML should look something like this
<subject_datas type="array">
<subject_data>
...
</subject_data>
<subject_data>
...
</subject_data>
</subject_datas>
The problem is the type attribute for the subject_datas tag. What i tried was to design it as a derived List and attach a property with a XMLAttribute attribute like this
[XmlRoot(ElementName = "subject_datas")]
public class SubjectDatas : List<SubjectData>
{
public SubjectDatas (IEnumerable<SubjectData> source)
{
this.AddRange(source);
Type = "array";
}
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}
But because the class is a Collection the XMLSerializer will just serialize the objects in the Collection not the Collection itself. So my Type property gets ignored :(
You could use composition over inheritance