I had the same problem, but with an additional restriction: there is no access to the source for the Domain or Derived classes. A derived object inherited from a Domain object, and since classes have the same unqualified name, the XmlSerializer will not be able to serialize an object of the Derived class.
Perhaps this could be resolved with brute force (changing the IL for one of the classes to add the appropriate attributes), but I need something “cleaner”. Here's what worked:
var attrs = new XmlAttributes(); attrs.XmlType = new XmlTypeAttribute("anythingButClassA"); var overrides = new XmlAttributeOverrides(); overrides.Add(typeof(Domain.ClassA), attrs); var serializer = new XmlSerializer(typeof(Derived.ClassA), overrides); serializer.Serialize(Console.Out, new Derived.ClassA());
Even if you are the author of the Domain and / or Derived classes, this way you do not need to rename them.
source share