Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Have you ever tried to xml serialize a class hierarchy derived from CollectionBase?

If you ever tried to serialize and deserialize a hierachy of classes starting a CollectionBase, you found that it doesn't work.

Consider a hierarchy like:

[XmlInclude(“DerivedCollection“)]
public class MyBase : CollectionBase
{
 // implement Add and Item and everything else you need
}

public class DerivedCollection : MyBase
{
 // implement Add and Item and everything else you need
}

If you serialize it with an XmlSerializer instantiated for the base class:

XmlSerializer serializer = new XmlSerializer( typeof( MyBase ) );

You will not be able to deserialize the XML properly. However, it gets worse if you try to serialize and deserialize another class that defines a field of MyBase. Read all the details of the problem here.

No Comments