Liquid XML Objects (C#, Visual Basic .Net) / Using the Generated Code / Reading XML
In This Topic
    Reading XML
    In This Topic

    Liquid XML Objects generates an XML Data Model from an XML Schema with functionality to deserialize XML documents in to the XML Data Model. Consider the following XML Schema:

     Sample XML Schema

            

     

    Bookstore.xsd
    Copy Code
    <?xml version="1.0" encoding="utf-8" ?>
    <!--Created with Liquid Studio (https://www.liquid-technologies.com)-->
    <xsd:schema xmlns:bs="http://www.liquid-technologies.com/sample/bookstore" elementFormDefault="qualified" targetNamespace="http://www.liquid-technologies.com/sample/bookstore" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:complexType name="bookstoreType">
            <xsd:sequence>
                <xsd:element name="book" type="bs:bookType" minOccurs="0" maxOccurs="unbounded" />
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="bookType">
            <xsd:sequence>
                <xsd:element name="title">
                    <xsd:annotation>
                        <xsd:documentation>The title of the book. 
    Max 50 characters.</xsd:documentation>
                    </xsd:annotation>
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:maxLength value="50" />
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
                <xsd:element name="author" type="bs:authorName" />
                <xsd:element name="genre" minOccurs="0">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="Reference" />
                            <xsd:enumeration value="Science fiction" />
                            <xsd:enumeration value="Action and Adventure" />
                            <xsd:enumeration value="Romance" />
                            <xsd:enumeration value="Mystery" />
                            <xsd:enumeration value="Horror" />
                            <xsd:enumeration value="Health" />
                            <xsd:enumeration value="Travel" />
                            <xsd:enumeration value="History" />
                            <xsd:enumeration value="Fantasy" />
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
            <xsd:attribute name="price" use="required">
                <xsd:simpleType>
                    <xsd:restriction base="xsd:decimal">
                        <xsd:fractionDigits value="2" />
                    </xsd:restriction>
                </xsd:simpleType>
            </xsd:attribute>
            <xsd:attribute name="publicationdate" type="xsd:date" />
            <xsd:attribute name="ISBN" type="xsd:string" />
        </xsd:complexType>
        <xsd:element name="bookstore" type="bs:bookstoreType" />
        <xsd:complexType name="authorName">
            <xsd:sequence>
                <xsd:element name="first-name" type="xsd:string">
                    <xsd:annotation>
                        <xsd:documentation>The authors first name.
    Max 50 characters.</xsd:documentation>
                    </xsd:annotation>
                </xsd:element>
                <xsd:element name="last-name" type="xsd:string" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:schema>
    
     Sample Reader Code

    The following code illustrates how to create a generated XML Data Model object from an XML document.

    The LxSerializer class performs the work of deserializing the XML data and constructing the XML Data Model object.

    Writing data into the generated classes
    Copy Code
    LxSerializer<BookstoreElm> serializer = new LxSerializer<BookstoreElm>();
    BookstoreElm bookstore = serializer.Deserialize(@"..\..\BookstoreSample.xml");
    
    // Display details about the books
    foreach (var book in bookstore.Books)
        Console.WriteLine($"Book : {book.Title} by {book.Author.First_Name} {book.Author.Last_Name} published on {book.Publicationdate}");
    

    Once the bookstore object has been deserialized it contains all the data loaded from the XML document, errors will have been raised for any invalid content (see Error Handling and Unexpected Elements). The objects properties can be read from and modified, it can then be serialized back to XML (see Writing XML) using the and the LxSerializer object.

     

    This example code is generated using the default 'Simplified Model', other model options are explored in Code Generation Models.
     Sample XML Document
    BookstoreSample.xml
    Copy Code
    <?xml version="1.0" encoding="utf-8"?>
    <bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.liquid-technologies.com/sample/bookstore">
      <book price="4.99" publicationdate="2008-10-20" ISBN="978-0747596837">
        <title>The Graveyard Book</title>
        <author>
          <first-name>Neil</first-name>
          <last-name>Gaiman</last-name>
        </author>
        <genre>Horror</genre>
      </book>
      <book price="21.99" publicationdate="2002-05-14" ISBN="0-596-00252-1">
        <title>XML Schemas</title>
        <author>
          <first-name>Eric</first-name>
          <last-name>van der Vlist</last-name>
        </author>
        <genre>Reference</genre>
      </book>
      <book price="5.99" publicationdate="1999-05-02" ISBN="0-945-16546-1">
        <title>The Portable Door</title>
        <author>
          <first-name>Tom</first-name>
          <last-name>Holt</last-name>
        </author>
        <genre>Reference</genre>
      </book>
    </bookstore>
    

     

     

    LxSerializer

    The LxSerializer uses the meta data attributes associated with the generated class to serialize and de-serialize them to XML.

    The easiest way to use the LxSerializer class is via the generic wrapper LxSerializer<T>. The templated version does some of the configuration and casting making it easier to work with.

    The LxSerializer object is quite expensive to create, and LxSerializer<T> will implicitly create an LxSerializer instance in its constructor (unless one is explicitly supplied). So if your code repeatedly creates instances of LxSerializer<T> (for the same or different types of T), then you should consider explicitly creating an instance of  LxSerializer and passing it to the constructor of LxSerializer<T>.

    LxSerializer (and thus LxSerializer<T>) are not thread safe. i.e. don't share them between threads.