Liquid XML Objects (C#, Visual Basic .Net) / XML Data Binding Introduction
In This Topic
    XML Data Binding Introduction
    In This Topic

    What is XML Data Binding

    XML Data Binding is the process of creating an XML Data Model from an XML Schema (XSD). This XML Data Model contain strongly typed classes and properties mirroring those defined in the XML Schema. Using the XML Data Model to read and write XML documents  is a quicker, easier and less error prone way of working with XML data.

    Liquid XML Objects provides highly accurate conversion between XML and the generated class model. Allowing complex XML standards to be supported, and includes support for XSD 1.0 and XSD 1.1.

    Generating code from an XSD

    The first step is to generate an XML Data Model from your XML Schema (XSD).

    This can be done Visual Studio or via the command line.

    In Visual Studio simply include the XSD within your and right click the "Generate Data Binding Code" menu item, that's it your done!


    The generated code is dependant on the Nuget 'LiquidTechnologies.XmlObjects.Runtime'
    IMPORTANT: The Nuget version must match the version shown in the LxRuntimeRequirements within the generated code.

    Using the generated code

    Once you have generated an XML Data Model, using it couldn't be simpler. We will read an XML document. This is achieved by using the LxSerializer< T> factory class, which will create an instance of our object.

    De-serializing and XML document
    Copy Code
    LxSerializer<BookstoreElm> serializer = new LxSerializer<BookstoreElm>();
    BookstoreElm bookstore = serializer.Deserialize(@"..\..\BookstoreSample.xml");

    Then simple use the strongly typed properties to read data from the object.

    Reading data from the generated classes
    Copy Code
    // Display details about the books
    foreach (var book in bookstore.Book)
        Console.WriteLine($"Book : {book.Title} by {book.Author.First_Name} {book.Author.Last_Name} published on {book.Publicationdate}");

     Data within the class library can be modified in the same way.

    Writing data into the generated classes
    Copy Code
    // Reduce the price of all reference books
    foreach (var book in bookstore.Book.Where(b => b.Genre == BookTypeCt.GenreEnum.Reference))
        book.Price = book.Price / 2;

    The XML data can then be written back out using the LxSerializer<T> we created earlier.

    Serializing the classes as XML data
    Copy Code
    serializer.Serialize(@"..\..\UpdatedBookstoreSample.xml", bookstore);

    And that's it in a nutshell. Simple, clean and easy. The generated code makes working with XML data simple and easy, whilst being extremely light weight and fast. This page provides a very quick overview of the functionality available. To learn more see Code Generation and Using the Generated Code.