XML Data Binding for VB.Net

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

Sample XML File - BookStoreSample.xml - Click to View

Serializing the XML (Objects to XML) - Code to create an XML document using XML Data Binding Objects - Click to View

Using XML Data Binding

Using the standard DOM approach

1Dim p As New Person()
2p.Name = "Fred"
3p.DateOfBirth = New XmlDateTime(1978, 6, 26)
4
5Dim a As New Address()
6p.Address = a
7p.Address.HouseNo = 7
8p.Address.PostCode = "WV6 6JY"
9
10Dim runAroundCar As New Car()
11p.Cars.Add(runAroundCar)
12runAroundCar.Model = "Ford"
13runAroundCar.Make = "Escort"
14
15Dim toyCar As New Car()
16p.Cars.Add(toyCar)
17toyCar.Model = "Lotus"
18toyCar.Make = "Elise"
19
20Debug.WriteLine(p.ToXml())
21
22p.ToXmlFile("SampleFile.xml")
1Dim xmlDoc As New XmlDocument()
2Dim xmlElmPerson As XmlElement = xmlDoc.CreateElement("Person")
3xmlDoc.AppendChild(xmlElmPerson)
4
5Dim xmlElmName As XmlElement = xmlDoc.CreateElement("Name")
6xmlElmPerson.AppendChild(xmlElmName)
7xmlElmName.InnerText = "Fred"
8
9Dim xmlElmDOB As XmlElement = xmlDoc.CreateElement("DateOfBirth")
10xmlElmPerson.AppendChild(xmlElmDOB)
11xmlElmDOB.InnerText = "1978-06-26"
12
13Dim xmlElmAddress As XmlElement = xmlDoc.CreateElement("Address")
14xmlElmPerson.AppendChild(xmlElmAddress)
15
16Dim xmlElmHouseNo As XmlElement = xmlDoc.CreateElement("HouseNo")
17xmlElmAddress.AppendChild(xmlElmHouseNo)
18xmlElmHouseNo.InnerText = "7"
19
20Dim xmlElmPostCode As XmlElement = xmlDoc.CreateElement("PostCode")
21xmlElmAddress.AppendChild(xmlElmPostCode)
22xmlElmPostCode.InnerText = "WV6 6JY"
23
24Dim xmlElmCarRA As XmlElement = xmlDoc.CreateElement("Car")
25xmlElmPerson.AppendChild(xmlElmCarRA)
26
27Dim xmlElmRAModel As XmlElement = xmlDoc.CreateElement("Model")
28xmlElmCarRA.AppendChild(xmlElmRAModel)
29xmlElmRAModel.InnerText = "Ford"
30
31Dim xmlElmRAMake As XmlElement = xmlDoc.CreateElement("Make")
32xmlElmCarRA.AppendChild(xmlElmRAMake)
33xmlElmRAMake.InnerText = "Escort"
34
35Dim xmlElmCarToy As XmlElement = xmlDoc.CreateElement("Car")
36xmlElmPerson.AppendChild(xmlElmCarToy)
37
38Dim xmlElmToyModel As XmlElement = xmlDoc.CreateElement("Model")
39xmlElmCarToy.AppendChild(xmlElmToyModel)
40xmlElmToyModel.InnerText = "Lotus"
41
42Dim xmlElmToyMake As XmlElement = xmlDoc.CreateElement("Make")
43xmlElmCarToy.AppendChild(xmlElmToyMake)
44xmlElmToyMake.InnerText = "Elise"
45
46Using sr As New StringWriter()
47  Dim xws As New XmlWriterSettings()
48  xws.Indent = True
49
50  Using xtw As XmlWriter = XmlTextWriter.Create(sr, xws)
51    xmlDoc.WriteTo(xtw)
52  End Using
53  Debug.WriteLine(sr.ToString())
54End Using
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.

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

1Dim p As New Person()
2
3' Load the XML
4p.FromXmlFile("SampleFile.xml")
5
6Debug.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
14Debug.WriteLine(String.Format( _
15  "Cars Owned ({0})", p.Cars.Count))
16For Each c As Car In p.Cars
17  Debug.WriteLine(String.Format( _
18    " {0}, {1}", c.Make, c.Model))
19Next
1Dim xmlDoc As New XmlDocument()
2xmlDoc.Load("SampleFile.xml")
3
4Dim xmlElmPerson As XmlElement = GetFirstElement(xmlDoc)
5If xmlElmPerson Is Nothing OrElse xmlElmPerson.Name <> "Person" Then
6  Throw New Exception("Must start with Person")
7End If
8
9Dim xmlElmName As XmlElement = GetFirstElement(xmlElmPerson)
10If xmlElmName Is Nothing OrElse xmlElmName.Name <> "Name" Then
11  Throw New Exception("Missing Person->Name")
12End If
13
14Dim xmlElmDOB As XmlElement = GetNextElement(xmlElmName)
15If xmlElmDOB Is Nothing OrElse xmlElmDOB.Name <> "DateOfBirth" Then
16  Throw New Exception("Missing Person->DateOfBirth")
17End If
18
19Dim xmlElmAddress As XmlElement = GetNextElement(xmlElmDOB)
20If xmlElmAddress Is Nothing OrElse xmlElmAddress.Name <> "Address" Then
21  Throw New Exception("Missing Person->Address")
22End If
23
24
25Dim xmlElmHouseNo As XmlElement = GetFirstElement(xmlElmAddress)
26If xmlElmHouseNo Is Nothing OrElse xmlElmHouseNo.Name <> "HouseNo" Then
27  Throw New Exception("Missing Person->Address->HouseNo")
28End If
29
30Dim xmlElmPostCode As XmlElement = GetNextElement(xmlElmHouseNo)
31If xmlElmPostCode Is Nothing OrElse xmlElmPostCode.Name <> "PostCode" Then
32  Throw New Exception("Missing Person->Address->PostCode")
33End If
34
35If GetNextElement(xmlElmPostCode) IsNot Nothing Then
36  Throw New Exception("Unexpected Element found")
37End If
38
39Debug.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
46Dim xmlElmCar As XmlElement = GetNextElement(xmlElmAddress)
47While 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)
70End While 1Private 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
11End Function

12
13Private 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
21End 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.

Liquid XML Studio

Starter Edition


Liquid XML Studio

Designer Edition


Liquid XML Studio

Developer Edition