I am trying to serialize a series of subclasses deriving from a single abstract superclass WorldObject
to XML, using the IXmlSerializable
interface. I have to use this interface, as I want the class to subscribe to some events as it is deserializing.
Using this answer, I came up with the following code:
[XmlInclude(typeof(SubType1))
,XmlInclude(typeof(SubType2))] // etc, includes all subtypes
public abstract class WorldObject : IComparable, IXmlSerializable
public void WriteXml(System.Xml.XmlWriter writer)
{
Type[] extraTypes = new Type[8];
// (...) adding types to array here
var worldObjectSerializer = new XmlSerializer(typeof(List<WorldObject>), extraTypes);
}
The last line where the XmlSerializer
is initialized throws a runtime error: System.MissingMethodException: Cannot create an abstract class 'Namespace.WorldObject'. The same error is thrown when I try to use typeof(WorldObject)
instead of typeof(List<WorldObject>)
.
I have also tried setting the XMLRoot to the same value in every class as demonstrated here, but the error persists.