Ok, this is getting really frustrating. I frequently find myself needing to implement Xml Schemas within solutions I am building. I spent years working with ASP.old and VB.old, so I really like the whole notion of "Strongly-Typed", and I therefore want to generate code behind an XSD.
The first attempt for most people is to use XSD.exe, or MSDataSetGenerator in Visual Studio (which calls XSD.exe under the covers) to generate a dataset or classes from the XSD. Unfortunately, the functionality within XSD.exe is limited to what can be described in a database structure. Trying to work with more complex XSDs lead to dreaded error messages, like "The same table (row) cannot be the child table in two nested relations."
The next attempt is to search the web, thereby finding references to tools such as CodeXS. While these are great tools, they are not without their drawbacks either.
Take for example the following snippet:
<xsd:choice>
<xsd:sequence>
<xsd:element
name="USERID"
type="ofx:IdType" />
<xsd:element
name="USERPASS"
type="ofx:PasswordType" />
</xsd:sequence>
<xsd:element
name="USERKEY"
type="ofx:UserKeyType" />
</xsd:choice>
CodeXS generates this as:
<System.Xml.Serialization.XmlElementAttribute("USERID", GetType(String), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlElementAttribute("USERKEY", GetType(String), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
System.Xml.Serialization.XmlElementAttribute("USERPASS", GetType(String), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified),_
System.Xml.Serialization.XmlChoiceIdentifierAttribute("_ItemsElementName")> _
Public
Property _Items() As
String()
Get
Return StringCollection.ToArray(Me.__Items)
End
Get
Set
Me.__Items = StringCollection.FromArray(value)
End
Set
End
Property
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public
Property Items() As StringCollection
Get
If (Me.__Items Is
Nothing) Then
Me.__Items = New StringCollection
End
If
Return
Me.__Items
End
Get
Set
Me.__Items = value
End
Set
End
Property
While all the items are there, they are not exactly strongly typed. Upon digging further into the schema, and how it was generated, it appears that the generators have issues with elements using restricted SimpleTypes. I could certainly change the code to include each item as a property, but that defeats the purpose of "auto-generated".
Further, there is no support for the choice element, which defines that the message can include the USERID and USERPASS elements OR the USERKEY element.
This leads me to my final question; whose fault is it? Are the schema suppliers wrong? I don't think so, all of the schemas I have had problems with DO validate against the Xml Schema specification. So that leaves the developer tools. I have yet to find one that works perfectly. Mainly, I have used Visual Studio, Altova XMLSpy and CodeXS. All of which are excellent tools, but none of which were completely accurate.
For the record, here are the schemas and product version I refer to above:
Schemas:
- OFX (Open Financial eXchange)
- BPEL (Business Process Execution Language
Tools:
- CodeXS Version 0.57 Beta
- Visual Studio 2005 SP1
- Altova XMLSpy 2006 rel. 3