Liquid XML Data Binding - A Full Example

This example will show the code generated produced from a simple schema. Along with sample code showing how to use it to read and write a simple XML Document.

Also included in the download is the simple object viewer. This allows you to explorer a generated class library, and is an excellent tool for prototyping, and evaluating the generated output.

The Schema

We will generate the class libraries using the following XSD. This is done using the XML Data Binding Wizard.

Examples - Source Code From XSD
A Full Example - Including all Source
Source Code from <xs:all>
Source Code from <xs:choice>
Source Code from <xs:sequence>
Effects of Cardinality (min/maxOccurs)
Effects of <xs:extension>
How Different Types are Dealt With

 
Download the source and generated sample code.
Download Source Zip

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xsd:element name="bookstore" type="bookstoreType"/>
    <xsd:complexType name="bookstoreType">
        <xsd:sequence maxOccurs="unbounded">
            <xsd:element name="book" type="bookType"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="bookType">
        <xsd:sequence>
            <xsd:element name="title" type="xsd:string"/>
            <xsd:element name="author" type="authorName"/>
            <xsd:element name="price" type="xsd:decimal"/>
        </xsd:sequence>
        <xsd:attribute name="genre" type="xsd:string"/>
        <xsd:attribute name="publicationdate" type="xsd:date"/>
        <xsd:attribute name="ISBN" type="xsd:string"/>
    </xsd:complexType>
    <xsd:complexType name="authorName">
        <xsd:sequence>
            <xsd:element name="first-name" type="xsd:string"/>
            <xsd:element name="last-name" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

(Figure 1a - The code for our sample XSD schema)


(Figure 1b - A graphical representation of the schema in 1a)

The Sample XML File

The following sample XML will be used for the following examples

<bookstore>
    <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
</bookstore>


The Generated Output

Expand each section to see a summary of the generated code, including sample code showing how to read and write an XML document. Note this is the simplified version of the source, all comments, private and protected methods have been removed for clarity. The full source code can be download.

Generated C# Classes
Generated C++ Classes
Generated Java Classes
Generated Visual Basic Classes

The Viewer Displaying the Sample

If you download the source, included in the zip is an object viewer. This viewer is a graphical representation of the classes/objects contained within the library. You can use the viewer to change the values of properties within the class library, it will even provide the code necessary to re-construct the XML that is currently loaded into the library. This is a useful way to familiarize yourself with the structure of a generated library

The Sample XSD, Sample XML, Generated code and object viewer, can be downloaded here
(The viewer requires the .Net framework to be installed)

Products | Free XSD Editor | Free XML Editor | XML Data Binding | Download | Purchase | Support | Contact Us
©2007 Liquid Technologies Limited. All rights reserved. Privacy Policy