I have a list of objects that contains two items. Each object contain several properties. My problems is that I have to serialize both items into xml but with different attributes for both elements.
I've already tried XmlAttributeOverrides
Example class:
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class Apple
{
[System.Xml.Serialization.XmlElementAttribute(DataType="nonNegativeInteger")]
public int Index{ get; set;}
[System.Xml.Serialization.XmlAttributeAttribute()]
public int Size{ get; set;}
[System.Xml.Serialization.XmlAttributeAttribute()]
public decimal Weight{ get; set;}
}
void Test()
{
List<Apple> apples = new List<Apple>
{
new Apple {Index = 0, Size = 1},
new Apple {Index = 1, Weight = 4}
};
}
Result after serialization:
<Apple Size=1> < Index>0</Index> </Apple> <Apple Weight=4> < Index>1</Index> </Apple>