Liquid XML Data Binding for .Net C#

  • Generates simple intuitive code
  • Supports XSD, XDR & DTD's
  • Unrivaled W3C XSD standard support, handles the most complex XML standards where xsd.exe would fail.
  • .Net Data Binding support
  • Generates code for
  • Supports Fast Infoset XML compression
  • Generates HTML Documentation
  • Royalty free distribution & source

Strongly Typed Code

Liquid XML Data Binder creates simple intuitive code from your XML Schema. The code library contains strongly typed classes and collections.

Customisable Code

The generated output may also be modified within special 'Hand Coded Blocks', changes inside these blocks are persisted when the library is re-generated, allowing the library to be customised to fit the projects needs.

Standard Support

The use of XML is exploding, and with it the number and complexity of 3rd party standards. There are now 1000's of XML Schemas describing everything from Solar orbits to coffee bean quality. Because the XSD standard is complex, many XML Data Binding tools only support a sub set of it. This may be adequate for small in house schemas, but in the real world you need something that will cope with the standards that exist out in the wild. Liquid XML Data Binder has an unprecedented level of support for the W3C XSD Standard allowing it to cope with the most complex standards, just try it and see.

Get Started

Download the free 30 day trial and get started now.

A Simple Example

The following example we will use the Person.xsd, shown below to demonstrate how to read and write an XML document based on this schema.

 Source XML Schema - Person.xsd

The Person.xsd schema shown graphically using Liquid XML Studio

The model for Person.xsd
<?xml version="1.0" encoding="utf-16"?>
<!--Created with Liquid XML Studio -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="Person">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="Name" type="xs:string" />
    <xs:element name="DateOfBirth" type="xs:date" />
    <xs:element minOccurs="0" name="Address">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="HouseNo" type="xs:int" />
       <xs:element name="PostCode" type="xs:string" />
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element minOccurs="0" maxOccurs="unbounded" name="Car">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="Make" type="xs:string" />
       <xs:element name="Model" type="xs:string" />
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>
        

 Sample XML File - PersonSample.xml

This XML file conforms to the Person.xsd shown above.
<?xml version="1.0"?>
<Person>
  <Name>Fred</Name>
  <DateOfBirth>1978-06-26</DateOfBirth>
  <Address>
    <HouseNo>7</HouseNo>
    <PostCode>WV6 6JY</PostCode>
  </Address>
  <Car>
    <Make>Escort</Make>
    <Model>Ford</Model>
  </Car>
  <Car>
    <Make>Elise</Make>
    <Model>Lotus</Model>
  </Car>
</Person> 

Creating an XML document - C# Source Code 

With Liquid XML Data Binder

The code needed to create the PersonSample.xml document using Xml Data Binding.

Person p = new Person();
p.Name = "Fred";
p.DateOfBirth = new XmlDateTime(1978, 6, 26);

Address a = new Address();
p.Address = a;
p.Address.HouseNo = 7;
p.Address.PostCode = "WV6 6JY";

Car runAroundCar = new Car();
p.Cars.Add(runAroundCar);
runAroundCar.Model = "Ford";
runAroundCar.Make = "Escort";

Car toyCar = new Car();
p.Cars.Add(toyCar);
toyCar.Model = "Lotus";
toyCar.Make = "Elise";

Debug.WriteLine(p.ToXml());

p.ToXmlFile("SampleFile.xml");

Without Data Binding

The code needed to create the PersonSample.xml document using MSXML.

XmlDocument xmlDoc = new XmlDocument();
XmlElement xmlElmPerson = xmlDoc.CreateElement("Person");
xmlDoc.AppendChild(xmlElmPerson);

XmlElement xmlElmName = xmlDoc.CreateElement("Name");
xmlElmPerson.AppendChild(xmlElmName);
xmlElmName.InnerText = "Fred";

XmlElement xmlElmDOB = xmlDoc.CreateElement("DateOfBirth");
xmlElmPerson.AppendChild(xmlElmDOB);
xmlElmDOB.InnerText = "1978-06-26";

XmlElement xmlElmAddress = xmlDoc.CreateElement("Address");
xmlElmPerson.AppendChild(xmlElmAddress);

XmlElement xmlElmHouseNo = xmlDoc.CreateElement("HouseNo");
xmlElmAddress.AppendChild(xmlElmHouseNo);
xmlElmHouseNo.InnerText = "7";

XmlElement xmlElmPostCode = xmlDoc.CreateElement("PostCode");
xmlElmAddress.AppendChild(xmlElmPostCode);
xmlElmPostCode.InnerText = "WV6 6JY";

XmlElement xmlElmCarRA = xmlDoc.CreateElement("Car");
xmlElmPerson.AppendChild(xmlElmCarRA);

XmlElement xmlElmRAModel = xmlDoc.CreateElement("Model");
xmlElmCarRA.AppendChild(xmlElmRAModel);
xmlElmRAModel.InnerText = "Ford";

XmlElement xmlElmRAMake = xmlDoc.CreateElement("Make");
xmlElmCarRA.AppendChild(xmlElmRAMake);
xmlElmRAMake.InnerText = "Escort";

XmlElement xmlElmCarToy = xmlDoc.CreateElement("Car");
xmlElmPerson.AppendChild(xmlElmCarToy);

XmlElement xmlElmToyModel = xmlDoc.CreateElement("Model");
xmlElmCarToy.AppendChild(xmlElmToyModel);
xmlElmToyModel.InnerText = "Lotus";

XmlElement xmlElmToyMake = xmlDoc.CreateElement("Make");
xmlElmCarToy.AppendChild(xmlElmToyMake);
xmlElmToyMake.InnerText = "Elise";

using (StringWriter sr = new StringWriter())
{
  XmlWriterSettings xws = new XmlWriterSettings();
  xws.Indent = true;

  using (XmlWriter xtw = XmlTextWriter.Create(sr, xws))
 {
    xmlDoc.WriteTo(xtw);
  }
  Debug.WriteLine(sr.ToString());
}

The XML Data Binding code is much easier to read, more concise and will allow errors to be detected at compile time should the data model change.

The DOM code is verbose, difficult to read, and should the data model change, the errors will only be picked up in testing.

  Reading an XML document - C# Source Code

The code needed to read the PersonSample.xml document using Xml Data Binding.
p.FromXmlFile("SampleFile.xml");

Debug.WriteLine(
 String.Format(
  "{0} was born {1}, and lives at {2}, {3}",
  p.Name,
  p.DateOfBirth.ToString(),
  p.Address.HouseNo,
  p.Address.PostCode));

Debug.WriteLine( 
 String.Format(
  "Cars Owned ({0})", p.Cars.Count));
foreach (Car c in p.Cars)
{
 Debug.WriteLine(
  string.Format(" {0}, {1}",
  c.Make,
  c.Model));
}
The code needed to read the PersonSample.xml document using MSXML.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("SampleFile.xml");

XmlElement xmlElmPerson = GetFirstElement(xmlDoc);
if (xmlElmPerson == null || xmlElmPerson.Name != "Person")
  throw new Exception("Must start with Person");

XmlElement xmlElmName = GetFirstElement(xmlElmPerson);
if (xmlElmName == null || xmlElmName.Name != "Name")
  throw new Exception("Missing Person->Name");

XmlElement xmlElmDOB = GetNextElement(xmlElmName);
if (xmlElmDOB == null || xmlElmDOB.Name != "DateOfBirth")
  throw new Exception("Missing Person->DateOfBirth");

XmlElement xmlElmAddress = GetNextElement(xmlElmDOB);
if (xmlElmAddress == null || xmlElmAddress.Name != "Address")
  throw new Exception("Missing Person->Address");


XmlElement xmlElmHouseNo = GetFirstElement(xmlElmAddress);
if (xmlElmHouseNo == null || xmlElmHouseNo.Name != "HouseNo")
  throw new Exception("Missing Person->Address->HouseNo");

XmlElement xmlElmPostCode = GetNextElement(xmlElmHouseNo);
if (xmlElmPostCode == null || xmlElmPostCode.Name != "PostCode")
  throw new Exception("Missing Person->Address->PostCode");

if (GetNextElement(xmlElmPostCode) != null)
  throw new Exception("Unexpected Element found");

Debug.WriteLine(
  String.Format(
    "{0} was born {1}, and lives at {2}, {3}",
    xmlElmName.InnerText,
    xmlElmDOB.InnerText,
    xmlElmHouseNo.InnerText,
    xmlElmPostCode.InnerText));

XmlElement xmlElmCar = GetNextElement(xmlElmAddress);
while (xmlElmCar != null)
{
  if (xmlElmCar.Name != "Car")
    throw new Exception("Unknown element " + xmlElmCar.Name);

  XmlElement xmlElmMake = GetFirstElement(xmlElmCar);
  if (xmlElmMake == null || xmlElmMake.Name != "Make")
    throw new Exception("Missing Person->Car->Make");

  XmlElement xmlElmModel = GetNextElement(xmlElmMake);
  if (xmlElmModel == null || xmlElmModel.Name != "Model")
    throw new Exception("Missing Person->Car->Model");

  if (GetNextElement(xmlElmModel) != null)
    throw new Exception("Unexpected Element found");

  Debug.WriteLine(
    string.Format(" {0}, {1}",
    xmlElmMake.InnerText,
    xmlElmModel.InnerText));

  xmlElmCar = GetNextElement(xmlElmCar);
} 

private static XmlElement GetFirstElement(XmlNode xmlParent)
{
  if (xmlParent == null)
    return null;
  else if (xmlParent.FirstChild == null)
    return null;
  else if (xmlParent.FirstChild is XmlElement)
    return xmlParent.FirstChild as XmlElement;
  else
    return GetNextElement(xmlParent.FirstChild);
}

private static XmlElement GetNextElement(XmlNode xmlNode)
{
  while (xmlNode != null)
 {
    xmlNode = xmlNode.NextSibling;
    if (xmlNode is XmlElement)
      return xmlNode as XmlElement;
  }
  return null;
}
Because the XML Data Binding code does the bulk of the parsing for you, manipulating the XML objects is just a matter of dealing with collections of objects, no need to continually check the type of an item etc.

Conclusion

XML Data Binding makes it significantly easier to deal with XML documents from within your code, resulting in less code, which is simpler to read and maintain.

As the generated class library is strongly typed, it forms a kind of template for the developer, ensuring that the data created conforms to the underlying XML Schema.

The maintenance phase of a project also befits as XML Data Binding allows any errors introduced because of changes in the data model to be caught early at compile time, unlike weakly typed DOM trees, which will give no indication of a problem until runtime. These kinds of changes can be a major cause of bugs which can only be caught in testing. Being able to identify these at compile time can save huge amounts of time and effort.