Liquid XML Data Binder 2019
Writing XML
Liquid XML Objects (C#, Visual Basic .Net) > Using the Generated Code > Writing XML

Consider the following XML Schema

 Sample XML Schema
Copy Code
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid Studio 2018 (>
<xsd:schema xmlns:bs="" elementFormDefault="qualified" targetNamespace="" version="1.0" xmlns:xsd="">
    <xsd:complexType name="bookstoreType">
            <xsd:element name="book" type="bs:bookType" minOccurs="0" maxOccurs="unbounded" />
    <xsd:complexType name="bookType">
            <xsd:element name="title">
                    <xsd:documentation>The title of the book. 
Max 50 characters.</xsd:documentation>
                    <xsd:restriction base="xsd:string">
                        <xsd:maxLength value="50" />
            <xsd:element name="author" type="bs:authorName" />
            <xsd:element name="genre" minOccurs="0">
                    <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:attribute name="price" use="required">
                <xsd:restriction base="xsd:decimal">
                    <xsd:fractionDigits value="2" />
        <xsd:attribute name="publicationdate" type="xsd:date" />
        <xsd:attribute name="ISBN" type="xsd:string" />
    <xsd:element name="bookstore" type="bs:bookstoreType" />
    <xsd:complexType name="authorName">
            <xsd:element name="first-name" type="xsd:string">
                    <xsd:documentation>The authors first name.
Max 50 characters.</xsd:documentation>
            <xsd:element name="last-name" type="xsd:string" />
 Sample Writer Code

The following code illustrates how to create a new instance of bookstore from scratch and populate it.

In order to serialize the class to XML an LxSerializer object is required.

Writing data into the generated classes
Copy Code
BookstoreElm bookstore = new BookstoreElm();
BookTypeCt book = new BookTypeCt();
book.Seq.Title = "Artemis";
book.Seq.Author.Seq.First_Name = "Andy";
book.Seq.Author.Seq.Last_Name = "Weir";
book.Seq.Genre = BookTypeCt.GenreEnum.Science_Fiction;
book.Price = 7.99;
book.ISBN = "978-1-78-503025-3";
book.Publicationdate = LxDateTime.CreateDate(2017, 4, 23);


LxSerializer<BookstoreElm> serializer = new LxSerializer<BookstoreElm>();
using (StringWriter sw = new StringWriter())
    serializer.Serialize(sw, bookstore);


Looking at the code above you will notice that many of the values are referenced via the property 'Seq', this is because the code very closely mirrors the XSD structure, their are a number of approaches for simplifying the model, these are explored in Code Generation Models.

 XML Output

The output from the code above. 

Sample Output
Copy Code
<?xml version="1.0" encoding="utf-16"?>
<bookstore xmlns="">
  <book price="7.99" publicationdate="2017-04-23Z" ISBN="978-1-78-503025-3">
    <genre>Science fiction</genre>




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.