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

The code generator can output code in a number of different ways in order to suit your requirements.

 

Option Reading Writing Description
Simplified Model Ideal for reading Ideal when complete adherence to the source XML Schema is not required.
Ordering of child elements may not reflect the rules in the XML Schema and cardinality based validation rules may not be applied.
Creates a very simple structure that makes working with the generated classes very simple, but the simplified structure may not contain all the nuances of the source XML data, so XML data written out from it is likely to contain elements do not respect the cardinality or ordering rules in the source XML Schema.
High Conformance Model Requires the navigation of more intermediate classes to access the data Ideal for applications that require high levels of compliance to the source XML Schema. Generates a class structure that mirrors the source XML schema, nuances in structure of the source XML are maintained at the expense of navigating more intermediate classes.
High Conformance + Simple Accessors Ideal for reading Ideal for applications that require high levels of compliance to the source XML Schema. Provides the accurate structuring of data from "High Conformance Model" with the simple properties found in "Simplified Model". This is a best of both worlds, but does provide 2 routes to the same data.

Simplified Model

Child elements are Collapsed into the containing element. This produces very simple code for reading and writing XML data.

The resulting code is very simple, however it is not possible to affect the order of the child elements.

Furthermore the generated model may not enforce all the cardinality rules specified in the XML Schema.

Simple Sample Code
Copy Code
var s = new LxSerializer<RootElm>();
RootElm elm = new RootElm();
elm.A.Add("Value1");
elm.A.Add("Value2");
elm.B.Add("Value3");
elm.A.Add("Value4");
s.Serialize("SimpleOutput.xml", elm); 

C# Watch Window showing "elm"

SimpleOutput.xml
Copy Code
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <A>Value1</A>
  <A>Value2</A>
  <A>Value4</A>
  <B>Value3</B>
</Root>

 

Strict validation against the XSDs can be performed in addition to the validation performed during serialization see Validation using the original XML Schemas (XSDs) .

High Conformance Model

The structure of the XSD is mirrored in the generated code.

The generated code produces XML that is extremely compliant with the source XSD, allowing fine control over the output XML. 

In order to accomplish this level of control, the generated code is more verbose, meaning a little more code is required to use it.

 

 

High Conformance Sample Code
Copy Code
var s = new LxSerializer<RootElm>();
RootElm elm = new RootElm();

elm.Chc.Seq.Add(
    new RootElm.RootChcSeq()
    {
        A = "Value1",
        B = "Value2"
    });

elm.Chc.Seq.Add(
    new RootElm.RootChcSeq()
    {
        A = "Value3",
        B = "Value4"
    });
s.Serialize("ConformantOutput.xml", elm);

C# Watch Window showing "elm"

SimpleOutput.xml
Copy Code
<?xml version="1.0" encoding="utf-8"?>
<Root>
  <A>Value1</A>
  <B>Value2</B>
  <A>Value3</A>
  <B>Value4</B>
</Root>

 

 

High Conformance + Simple Accessors

The generated code is exactly the same as that generated with the 'High Conformance Model', but additional readonly accessors are generated making it possible to read data from the generated classes in the same way as you would the 'Simplified Model', but writing to the model is the same as the 'High Conformance Model'.

 

The addition of the simple accessors makes reading data from the classes more concise.

Reading data using Simple Accessors - Sample Code
Copy Code
// long hand
foreach (var seq in elm.Chc.Seq)
    Console.WriteLine($"B={seq.A}");

// using the simple accesors
foreach (string a in elm.A)
    Console.WriteLine($"A={a}");

 

C# Watch Window showing the simple read only accessors on "elm"

Legend

In the sample code above the Lx... attributes, qualifying namespaces and comments have been removed for clarity.
The Watch expressions have also been edited to make them more readable.