I never found a way to get rid of the exception, but I found a workaround. The solution simply writes its own element wrapper with the type as its name, calls the WriteXml function and writes the end element.
foreach (WorldObject wO in list)
{
writer.WriteStartElement(wO.GetType().ToString());
wO.WriteXml(writer);
writer.WriteEndElement();
}
This allows you to deserialize the concrete implementation of the abstract class as well. The wO.GetType().ToString()
creates a string with the concrete class and namespaces. Then you can create an XmlSerializer
where the type is gleaned from the element wrapper name, like so:
Type t = Type.GetType(reader.LocalName);
XmlSerializer worldObjectSerializer = new XmlSerializer(t, new XmlRootAttribute(reader.LocalName));