views:

233

answers:

1

When I try to serialize a populated instance of type List<C>() where:

public class A<T> : List<T>
{
     [XmlAttribute("Name")]
     public string Name {get; set;}
     public A() {}
}

public class B
{
    [XmlAttribute("Other")]
    public string OtherPCO { get; set:}
}

public class C : A<B>
{

}

The serialization drops the Name property of class A but does create an array of type B with the OtherPCO property. How can I get the serializer to include Name?

+3  A: 

Collections are serialized in a specific manner, which takes into account only the items of the collection, not the extra properties you added to the class. You need to wrap the collection in another class that is not a collection.

This should give you the desired result :

public class A<T>
{
     [XmlAttribute("Name")]
     public string Name {get; set;}

     [XmlElement(typeof(T))]
     public List<T> Items { get; set; }

}
Thomas Levesque
That's annoying! But thanks for the example.
Oliver