How different data types effect the generated code

Overview

The XSD containers <all>, <choice> and <sequence> can contain definitions for child elements.

  • If a child element contains a textual value (Primitive Type's), then it is represented as a strongly typed property in the generated code.
  • If the child element contains other elements or attributes (i.e. its a Complex Type), then a class is generated to represent it, and a property created exposing this new class.

This example will explore how different types they are dealt with.

Sample XSD

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="RootElm">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="StringType" type="xs:string"/>
                <xs:element name="intType" type="xs:int"/>
                <xs:element name="ComplexType">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="DateType" type="xs:dateTime"/>
                            <xs:element name="Base64Type" type="xs:base64Binary"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

The Generated Code

The code generated for the sample XSD is shown here in its UML representation, note there are 2 classes, one for the RootElm and one for the nested element ComplexType.

This is a stripped down version of the classes. Note that RootElm has a property ComplexType.

public class RootElm : LiquidTechnologies.LtXmlLib3.XmlObjectBase
{
    public String StringType { get... set...}
    public Int32 IntType { get... set...}
    public TypesLib.ComplexType ComplexType { get... set...}
}
public class ComplexType : LiquidTechnologies.LtXmlLib3.XmlObjectBase
{
    public LiquidTechnologies.LtXmlLib3.XmlDateTime DateType { get... set...}
    public LiquidTechnologies.LtXmlLib3.BinaryData Base64Type { get... set...}
}

Sample Code

The Code below shows how the resulting classes can be used to create an XML document.
Note the Properties of the ComplexType are addressed via the ComplexType property i.e. elm.ComplexType.DateType.

// create an instance of the class to load the XML file into
TypesLib.RootElm elm = new TypesLib.RootElm();
// set data into the element
elm.StringType = "Test String value";
elm.IntType = 5; // and the child element
elm.ComplexType.DateType.SetDateTime(2004, 4, 26, 10, 41, 35);
elm.ComplexType.Base64Type.SetData("075BCD15", BinaryData.Encoding.Hex);
// Lets look at the XML we produced.
Trace.WriteLine(elm.ToXml());

The output from the sample above

<?xml version="1.0"?>
<!--Created by Liquid XML Data Binding Libraries (www.liquid-technologies.com)-->
<RootElm xmlns:Xs="http://www.w3.org/2001/XMLSchema-instance">
    <StringType>Test String value</StringType>
    <intType>5</intType>
    <ComplexType>
        <DateType>2004-04-26T10:41:35</DateType>
        <Base64Type>cLXcUQ==</Base64Type>
    </ComplexType>
</RootElm>

Notes

If the ComplexType held within the RootElm was optional, then you would have to create and assign an object to elm.ComplexType before using it (see next item).