When a class is inherited from List, XmlSerializer doesn’t serialize other attributes

This is by design. I don’t know why this decision was made, but it is stated in the documentation:

  • Classes that implement ICollection or IEnumerable. Only collections are
    serialized, not public properties.

(Look under “Items that can be serialized” section). Someone has filed a bug against this, but it won’t be changed – here, Microsoft also confirms that not including the properties for classes implementing ICollection is in fact the behaviour of XmlSerializer.

A workaround would be to either:

  • Implement IXmlSerializable and control serialization yourself.

or

  • Change MyClass so it has a public property of type List (and don’t subclass it).

or

  • Use DataContractSerializer, which handles this scenario.

Leave a Comment