XmlSerializer serialize generic List of interface

You can use XmlSerializer as well, but you need to include all the possible types that can appear in the object graph you’re serializing, which limits extensibility and lowers maintainability. You can do it by using an overload of the constructor of XmlSerializer:

var x = new XmlSerializer(animals.GetType(), new Type[] { typeof(Cat), typeof(Dog) });

Also, there are several issues of note when using XmlSerializer, all of the outlined here (MSDN) – for example look under the heading ‘Dynamically generated assemblies’.

Leave a Comment