XmlInclude getting ignored
| December 14th, 2008So, when pointing the XmlSerializer at a document that contains a list of abstract base objects, I came across a fairly standard exception:
System.InvalidOperationException: There was an error generating the XML document. System.InvalidOperationException: The type
was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.
But, even after I put in the XmlIncludes, OR when using the serializer constructor overload that takes a list of types, I was still getting the error.
Turns out my problem, strangely, was this line:
[XmlRootAttribute( "Document", Namespace = "http://www.cuttlefishindustries.com/mycoolnamespace", IsNullable = false)]
I didn't investigate it that much but it appears that if you specify a Namespace, the serializer ignores the additional types you supply (either via XmlInclude or the constructor overload). Once I took out my document's XML root attribute, my serializer worked. Huh. So, there ya go, hopefully this'll save someone some time, sometime.