LINQ to XSD versus XSD.exe (part 1)

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:

While the second schema will generate classes:

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:

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:

If you have other suggestions? Don’t hesitate to post them as a comment to this blog item.

Read more from the Uncategorized category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.

Using LINQ to XSD, XML and SQL

Last year I started working on the Aincha Web Framework. This ASP.Net framework should make it possible to create websites with the use of widgets as building blocks. The configuration of the website is done by the use of XML.

With the use of XML configuration files, different page types can be defined which will be used to put different widgets inside the template placeholders. The same widget can be used by different page types.

I the first (proof of concept) version of the Aincha Web Framework I used XSD.exe to generate C# code which make it possible to deserialize XML files against these code generated classes. XSD.exe has some limitations when it comes to complex XML schemas. With the release of .Net 3.5 you can use LINQ (Language Integrated Query) which makes it possible to make use of datasources (like SQL, XML) directly from within the source code.

To continue with the Aincha Web Framework, to get a basic useful framework, I will do a little research on the use of LINQ to XSD and LINQ to XML for the XML configuration files. I will post the results on my blog.

Read more from the Uncategorized category. If you would like to leave a comment, click here: Comment. or stay up to date with this post via RSS, or you can Trackback from your site.