Reasons to Use XML Data Binding
| |
- Reduces Development Time
- Increased reliability (spot problems at compile time not runtime)
- Creates more readable code
- Fastest root between code and XML
|
|
Reasons To Use Liquid XML Data Binding Code Generator
| |
|
- Generates simple intuitive code
- Supports XSD, XDR & DTD's
- Unrivaled W3C XSD standard support, handles with the most complex XML standards where xsd.exe would fail.
- .Net Data Binding support
|
|
|
Xml Data Binding
XML Data Binding allows you to treat your XML documents as objects within your application. This makes dealing with XML data from a programming language a simple matter of manipulating these strongly typed objects.
Liquid XML Data Binding Code Generator takes an XML Schema and uses it to generate a class library. These classes map directly to to data contained in your XML Schema, so if your schema has an Person entity and that has a date of birth attribute, then a class called Person will be generated, which will have a property called DateOfBirth. |
|
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.
|
|
Tools Included with Liquid XML Studio
Liquid XML Data Binding Code Generator is included in the Developer Edition of Liquid XML Studio, which includes many other XML tools including
XSD Editor, XML Editor, XML Differencing tool, XPath Query viewer, Web Service Call Composer and more... see Liquid XML Studio.
|
 |
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.
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.
XML Schema - Bookstore.xsd - Click to View
Deserializing the XML (XML to Objects) - Code to read an XML document using XML Data Binding Objects - Click to View
Using XML Data Binding |
Using the standard DOM approach |
1 p.FromXmlFile("SampleFile.xml");
2
3 Debug.WriteLine(
4 String.Format(
5 "{0} was born {1}, and lives at {2}, {3}",
6 p.Name,
7 p.DateOfBirth.ToString(),
8 p.Address.HouseNo,
9 p.Address.PostCode));
10
11 Debug.WriteLine(
12 String.Format(
13 "Cars Owned ({0})", p.Cars.Count));
14 foreach (Car c in p.Cars)
15 ...{
16 Debug.WriteLine(
17 string.Format(" {0}, {1}",
18 c.Make,
19 c.Model));
20 }
|
1 XmlDocument xmlDoc = new XmlDocument();
2 xmlDoc.Load("SampleFile.xml");
3
4 XmlElement xmlElmPerson = GetFirstElement(xmlDoc);
5 if (xmlElmPerson == null || xmlElmPerson.Name != "Person")
6 throw new Exception("Must start with Person");
7
8 XmlElement xmlElmName = GetFirstElement(xmlElmPerson);
9 if (xmlElmName == null || xmlElmName.Name != "Name")
10 throw new Exception("Missing Person->Name");
11
12 XmlElement xmlElmDOB = GetNextElement(xmlElmName);
13 if (xmlElmDOB == null || xmlElmDOB.Name != "DateOfBirth")
14 throw new Exception("Missing Person->DateOfBirth");
15
16 XmlElement xmlElmAddress = GetNextElement(xmlElmDOB);
17 if (xmlElmAddress == null || xmlElmAddress.Name != "Address")
18 throw new Exception("Missing Person->Address");
19
20
21 XmlElement xmlElmHouseNo = GetFirstElement(xmlElmAddress);
22 if (xmlElmHouseNo == null || xmlElmHouseNo.Name != "HouseNo")
23 throw new Exception("Missing Person->Address->HouseNo");
24
25 XmlElement xmlElmPostCode = GetNextElement(xmlElmHouseNo);
26 if (xmlElmPostCode == null || xmlElmPostCode.Name != "PostCode")
27 throw new Exception("Missing Person->Address->PostCode");
28
29 if (GetNextElement(xmlElmPostCode) != null)
30 throw new Exception("Unexpected Element found");
31
32 Debug.WriteLine(
33 String.Format(
34 "{0} was born {1}, and lives at {2}, {3}",
35 xmlElmName.InnerText,
36 xmlElmDOB.InnerText,
37 xmlElmHouseNo.InnerText,
38 xmlElmPostCode.InnerText));
39
40 XmlElement xmlElmCar = GetNextElement(xmlElmAddress);
41 while (xmlElmCar != null)
42 ...{
43 if (xmlElmCar.Name != "Car")
44 throw new Exception("Unknown element " + xmlElmCar.Name);
45
46 XmlElement xmlElmMake = GetFirstElement(xmlElmCar);
47 if (xmlElmMake == null || xmlElmMake.Name != "Make")
48 throw new Exception("Missing Person->Car->Make");
49
50 XmlElement xmlElmModel = GetNextElement(xmlElmMake);
51 if (xmlElmModel == null || xmlElmModel.Name != "Model")
52 throw new Exception("Missing Person->Car->Model");
53
54 if (GetNextElement(xmlElmModel) != null)
55 throw new Exception("Unexpected Element found");
56
57 Debug.WriteLine(
58 string.Format(" {0}, {1}",
59 xmlElmMake.InnerText,
60 xmlElmModel.InnerText));
61
62 xmlElmCar = GetNextElement(xmlElmCar);
63 }
1 private static XmlElement GetFirstElement(XmlNode xmlParent)
2 ...{
3 if (xmlParent == null)
4 return null;
5 else if (xmlParent.FirstChild == null)
6 return null;
7 else if (xmlParent.FirstChild is XmlElement)
8 return xmlParent.FirstChild as XmlElement;
9 else
10 return GetNextElement(xmlParent.FirstChild);
11 }
12
13 private static XmlElement GetNextElement(XmlNode xmlNode)
14 ...{
15 while (xmlNode != null)
16 ...{
17 xmlNode = xmlNode.NextSibling;
18 if (xmlNode is XmlElement)
19 return xmlNode as XmlElement;
20 }
21 return null;
22 }
|
| Because the XML Data Binding code does the bulk of the parsing fo 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 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 weekly 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.
|