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 Dim p As New Person()
2
3 ' Load the XML
4 p.FromXmlFile("SampleFile.xml")
5
6 Debug.WriteLine(String.Format( _
7 "{0} was born {1}, " & _
8 "and lives at {2}, {3}", _
9 p.Name, _
10 p.DateOfBirth.ToString(), _
11 p.Address.HouseNo, _
12 p.Address.PostCode))
13
14 Debug.WriteLine(String.Format( _
15 "Cars Owned ({0})", p.Cars.Count))
16 For Each c As Car In p.Cars
17 Debug.WriteLine(String.Format( _
18 " {0}, {1}", c.Make, c.Model))
19 Next
|
1 Dim xmlDoc As New XmlDocument()
2 xmlDoc.Load("SampleFile.xml")
3
4 Dim xmlElmPerson As XmlElement = GetFirstElement(xmlDoc)
5 If xmlElmPerson Is Nothing OrElse xmlElmPerson.Name <> "Person" Then
6 Throw New Exception("Must start with Person")
7 End If
8
9 Dim xmlElmName As XmlElement = GetFirstElement(xmlElmPerson)
10 If xmlElmName Is Nothing OrElse xmlElmName.Name <> "Name" Then
11 Throw New Exception("Missing Person->Name")
12 End If
13
14 Dim xmlElmDOB As XmlElement = GetNextElement(xmlElmName)
15 If xmlElmDOB Is Nothing OrElse xmlElmDOB.Name <> "DateOfBirth" Then
16 Throw New Exception("Missing Person->DateOfBirth")
17 End If
18
19 Dim xmlElmAddress As XmlElement = GetNextElement(xmlElmDOB)
20 If xmlElmAddress Is Nothing OrElse xmlElmAddress.Name <> "Address" Then
21 Throw New Exception("Missing Person->Address")
22 End If
23
24
25 Dim xmlElmHouseNo As XmlElement = GetFirstElement(xmlElmAddress)
26 If xmlElmHouseNo Is Nothing OrElse xmlElmHouseNo.Name <> "HouseNo" Then
27 Throw New Exception("Missing Person->Address->HouseNo")
28 End If
29
30 Dim xmlElmPostCode As XmlElement = GetNextElement(xmlElmHouseNo)
31 If xmlElmPostCode Is Nothing OrElse xmlElmPostCode.Name <> "PostCode" Then
32 Throw New Exception("Missing Person->Address->PostCode")
33 End If
34
35 If GetNextElement(xmlElmPostCode) IsNot Nothing Then
36 Throw New Exception("Unexpected Element found")
37 End If
38
39 Debug.WriteLine(String.Format( _
40 "{0} was born {1}, and lives at {2}, {3}", _
41 xmlElmName.InnerText, _
42 xmlElmDOB.InnerText, _
43 xmlElmHouseNo.InnerText, _
44 xmlElmPostCode.InnerText))
45
46 Dim xmlElmCar As XmlElement = GetNextElement(xmlElmAddress)
47 While xmlElmCar IsNot Nothing
48 If xmlElmCar.Name <> "Car" Then
49 Throw New Exception("Unknown element " & xmlElmCar.Name)
50 End If
51
52 Dim xmlElmMake As XmlElement = GetFirstElement(xmlElmCar)
53 If xmlElmMake Is Nothing OrElse xmlElmMake.Name <> "Make" Then
54 Throw New Exception("Missing Person->Car->Make")
55 End If
56
57 Dim xmlElmModel As XmlElement = GetNextElement(xmlElmMake)
58 If xmlElmModel Is Nothing OrElse xmlElmModel.Name <> "Model" Then
59 Throw New Exception("Missing Person->Car->Model")
60 End If
61
62 If GetNextElement(xmlElmModel) IsNot Nothing Then
63 Throw New Exception("Unexpected Element found")
64 End If
65
66 Debug.WriteLine(String.Format(" {0}, {1}", _
67 xmlElmMake.InnerText, xmlElmModel.InnerText))
68
69 xmlElmCar = GetNextElement(xmlElmCar)
70 End While
1 Private Function GetFirstElement()Function GetFirstElement(ByVal xmlParent As XmlNode) As XmlElement
2 If xmlParent Is Nothing Then
3 Return Nothing
4 ElseIf xmlParent.FirstChild Is Nothing Then
5 Return Nothing
6 ElseIf TypeOf xmlParent.FirstChild Is XmlElement Then
7 Return TryCast(xmlParent.FirstChild, XmlElement)
8 Else
9 Return GetNextElement(xmlParent.FirstChild)
10 End If
11 End Function
12
13 Private Function GetNextElement()Function GetNextElement(ByVal xmlNode As XmlNode) As XmlElement
14 While xmlNode IsNot Nothing
15 xmlNode = xmlNode.NextSibling
16 If TypeOf xmlNode Is XmlElement Then
17 Return TryCast(xmlNode, XmlElement)
18 End If
19 End While
20 Return Nothing
21 End Function
|
| 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.
|