Now this is just plain annoying...
As I've mentioned before, I'm doing some ongoing work with code generation from XmlSchema files. Developers mark up XmlSchema documents with some extra attributes in our namespace, and that influences how the code gets generated. Think of it as xsd.exe, only this works.
So today a new problem was brought to my attention. I read in schema files using the .NET XmlSchema classes. OK, that works well. For any element in the schema, you can ask its XmlSchemaDatatype what the corresponding .NET value type would be. E.g. if you ask an element of type "xs:int", you get back System.Int32. "xs:dateTime" maps to System.DateTime, etc.
When you want to serialize .NET objects using the XmlSerializer, you can add extra custom attributes to influence the behavior of the serializer. So, if you have a property that returns a DateTime, but the schema type is "date" you need some help, since there's no underlying .NET Date type.
public class MyClass
public DateTime SomeDate
The XmlElementAttribute tells the serializer to enforce the rules corresponding to the schema datatype "date" and not "dateTime". Groovy so far.
However, the XmlSerializer only supports certain schema types for certain CLR types. Makes sense, as this
public Int32 SomeDate
doesn't really make any intuitive sense.
So now for the catch. The CLR types that the schema reading classes (System.Xml.Schema) map to schema types don't in all cases match the CLR types that the XmlSerializer maps to schema types. The schema reader says that "xs:integer" maps to System.Decimal. OK, that's consistent with the XmlSchema part 2 spec. Unfortunately, the XmlSerializer says that "xs:integer" must map to a System.String. So does "xs:gYear", and several others.
The end result is that I can't rely on the XmlSchemaDatatype class to tell me what type to make the resulting property in my generated code. Arrrrrggggghhhhh!!!!!!!
The two solutions are basically
- tell people not to use integer, gYear, etc. (possibly problematic)
- have my code embody the differences in type mapping between XmlSchema and XmlSerializer (lame)
I haven't delved, but I can only assume that xsd.exe uses the latter of the two.