While LINQ to XSD is still in alpha, it is always a good idea to look at the (new) possibilities of this new technology. Before LINQ to XSD I used the XSD.exe to generate source files based on a XML schema. By generating this code you can work with XML files on a typed safe manner.
I will try to compare the differents between LINQ to XSD and the XSD tool. I will look at the (generated) code needed to work with XML files and try to compare the performances of both techniques. Based on the Linq to XSD overview document I will use the following XML schema:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/Orders"
xmlns="http://www.example.com/Orders"
elementFormDefault="qualified">
<xs:element name="Batch">
<xs:complexType>
<xs:sequence>
<xs:element ref="PurchaseOrder" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="PurchaseOrder">
<xs:complexType>
<xs:sequence>
<xs:element name="CustId" type="xs:string"/>
<xs:element ref="Item" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Item">
<xs:complexType>
<xs:sequence>
<xs:element name="ProdId" type="xs:string"/>
<xs:element name="Price" type="xs:double"/>
<xs:element name="Quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Code generation difficulties
The XML Schema placed above is different than the one I start using by default (for the PageType XML Schema of the Aincha Web Framework). Because the Purchase orders schema is not that complex I would have implemented it this way:
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/Orders"
xmlns="http://www.example.com/Orders"
elementFormDefault="qualified">
<xs:element name="Batch">
<xs:complexType>
<xs:sequence>
<xs:element name="PurchaseOrder" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="CustId" type="xs:string"/>
<xs:element name="Item" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="ProdId" type="xs:string"/>
<xs:element name="Price" type="xs:double"/>
<xs:element name="Quantity" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
This schema has the same definition as the previous one only the PurchaseOrder and Item are part of the complexType parts. Although this schema’s are both valid and they look the same, when using this schema with the XSD tool, different code is generated.
The first schema will generate the classes:
- Batch
- PurchaseOrder
- OrderItem
While the second schema will generate classes:
- Batch
- BatchPurchaseOrder
- BatchPurchaseOrderItem
Which is in my opinion not the names you would expect. If the schema is way more complex, you get even bigger names. And I know for sure that programmers don’t want to work with this unusual names.
At first, I though that this was a limitation of the XSD tool. But using the second schema within LINQ to XSD, the class names are also different and generated as private classes of the Batch object. With LINQ to XSD you get the following:
- Batch
- Batch.PurchaseOrderLocalType
- Batch.PurchaseOrderLocalType.ItemLocalType
So it is important to keep it mind that different code will be generated bases on the structure of the XSD. Even when both schemas are valid.
What’s next?
In the next part of this comparison research I will investigate the following things:
- amount of code used to work with XML data (based on the generated schema code);
- time to load/parse the XML data (with different sizes);
- time to print all the items inside the purchase orders to the console;
- time to calculate the total purchase orders batch value and time to calculate the purchase order value for each customer;
- CPU and Memory consumptions;
If you have other suggestions? Don’t hesitate to post them as a comment to this blog item.



