Strange bug in XML Serialization/Mapping classes

Note: this entry has moved.

While trying to customize the generated code for an XmlSchema, using the technique explained in a previous post, I came to a point where I can't advance any further as the framework classes are throwing an exception where they shouldn't.
The customization process basically involves using the XmlSchemaImporter class and using its ImportTypeMapping() method. To further customize it, I wanted to modify the type members, therefore, I tried to use the ImportMembersMapping() with exactly the same (working) XmlQualifiedName used for the previous method. I got an InvalidCastException thrown in the face :S. Here's the damn stack trace:

System.InvalidCastException: Specified cast is not valid.
  at System.Xml.Serialization.XmlMembersMapping..ctor(TypeScope scope, ElementAccessor accessor)
  at System.Xml.Serialization.XmlSchemaImporter.ImportMembersMapping(XmlQualifiedName name)

MS, help!!! Calling code is simple:

// Works
XmlTypeMapping map = imp.ImportTypeMapping( element.QualifiedName );
// Blows
XmlMembersMapping members = imp.ImportMembersMapping( element.QualifiedName );

1 Comment

  • The classes marked "This type supports the .NET Framework infrastructure and is not intended to be used directly from your code." are ALWAYS the most interesting. Also I think code generators are somewhat different in that users of your system will never interact with them directly, only their output, so it is only you that has to deal with any problems/quirks.

Comments have been disabled for this content.