XML Data Binding for Visual Basic 6

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
  • Ideal for legacy integration projects

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.

No other product supports Visual Basic

Its sad to say to companies just aren't developing Visual basic tools anymore. Liquid is now the only, company with an XML Data Binding solution for Visual Basic 6.

Migration path to VB.Net

Because we support VB.Net and C# as well as VB6, any code you write for VB6 is easily ported to .Net, as the libraries interfaces are all but identical to the VB ones.

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.

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 - Person.xsd - Click to View

Sample XML File - PersonSample.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
 2Dim c As Car
 3
 4' Load the XML
 5p.FromXmlFile "SampleFile.xml"
 6
 7Debug.Print p.Name_ & " was born " & _
 8  p.DateOfBirth.ToString & ", and lives at " & _
 9  p.Address.HouseNo & ", " & _
10  p.Address.PostCode
11
12Debug.Print "Cars Owned (" & p.Cars.Count & ")"
13For Each c In p.Cars
14  Debug.Print " " & c.Make & ", " & c.Model
15Next
 1Dim xmlDoc As New MSXML2.DOMDocument
 2
 3xmlDoc.Load ("SampleFile.xml")
 4
 5Dim xmlElmPerson As MSXML2.IXMLDOMElement
 6Set xmlElmPerson = GetFirstElement(xmlDoc)
 7If xmlElmPerson Is Nothing Then
 8  Err.Raise -1, "Test", "Must start with Person"
 9ElseIf xmlElmPerson.nodeName <> "Person" Then
10  Err.Raise -1, "Test", "Unexpected element"
11End If
12
13Dim xmlElmName As MSXML2.IXMLDOMElement
14Set xmlElmName = GetFirstElement(xmlElmPerson)
15If xmlElmName Is Nothing Then
16  Err.Raise -1, "Test", "Missing Person->Name"
17ElseIf xmlElmName.nodeName <> "Name" Then
18  Err.Raise -1, "Test", "Unexpected element"
19End If
20
21Dim xmlElmDOB As MSXML2.IXMLDOMElement
22Set xmlElmDOB = GetNextElement(xmlElmName)
23If xmlElmDOB Is Nothing Then
24  Err.Raise -1, "Test", "Missing Person->DateOfBirth"
25ElseIf xmlElmDOB.nodeName <> "DateOfBirth" Then
26  Err.Raise -1, "Test", "Unexpected element"
27End If
28
29Dim xmlElmAddress As MSXML2.IXMLDOMElement
30Set xmlElmAddress = GetNextElement(xmlElmDOB)
31If xmlElmAddress Is Nothing Then
32  Err.Raise -1, "Test", "Missing Person->Address"
33ElseIf xmlElmAddress.nodeName <> "Address" Then
34  Err.Raise -1, "Test", "Unexpected element"
35End If
36
37Dim xmlElmHouseNo As MSXML2.IXMLDOMElement
38Set xmlElmHouseNo = GetFirstElement(xmlElmAddress)
39If xmlElmHouseNo Is Nothing Then
40  Err.Raise -1, "Test", "Missing Person->Address->HouseNo"
41ElseIf xmlElmHouseNo.nodeName <> "HouseNo" Then
42  Err.Raise -1, "Test", "Unexpected element"
43End If
44
45Dim xmlElmPostCode As MSXML2.IXMLDOMElement
46Set xmlElmPostCode = GetNextElement(xmlElmHouseNo)
47If xmlElmPostCode Is Nothing Then
48  Err.Raise -1, "Test", "Missing Person->Address->PostCode"
49ElseIf xmlElmPostCode.nodeName <> "PostCode" Then
50  Err.Raise -1, "Test", "Unexpected element"
51End If
52
53If (GetNextElement(xmlElmPostCode) Is Nothing) = False Then
54  Err.Raise -1, "Test", "Unexpected element"
55End If
56
57Debug.Print xmlElmName.Text & " was born " & _
58  xmlElmDOB.Text & ", and lives at " & _
59  xmlElmHouseNo.Text & ", " & _
60  xmlElmPostCode.Text
61
62Dim xmlElmCar As MSXML2.IXMLDOMElement
63Set xmlElmCar = GetNextElement(xmlElmAddress)
64While (xmlElmCar Is Nothing) = False
65  If xmlElmCar.nodeName <> "Car" Then
66    Err.Raise -1, "Test", "Unexpected element"
67  End If
68
69  Dim xmlElmMake As MSXML2.IXMLDOMElement
70  Set xmlElmMake = GetFirstElement(xmlElmCar)
71  If xmlElmMake Is Nothing Then
72    Err.Raise -1, "Test", "Missing Person->Car->Make"
73  ElseIf xmlElmMake.nodeName <> "Make" Then
74    Err.Raise -1, "Test", "Unexpected element"
75  End If
76
77  Dim xmlElmModel As MSXML2.IXMLDOMElement
78  Set xmlElmModel = GetNextElement(xmlElmMake)
79  If xmlElmModel Is Nothing Then
80    Err.Raise -1, "Test", "Missing Person->Car->Model"
81  ElseIf xmlElmModel.nodeName <> "Model" Then
82    Err.Raise -1, "Test", "Unexpected element"
83  End If
84
85  If (GetNextElement(xmlElmModel) Is Nothing) = False Then
86    Err.Raise -1, "Test", "Unexpected Element"
87  End If
88
89  Debug.Print " " & _
90      xmlElmMake.Text & ", " & _
91      xmlElmModel.Text
92
93  Set xmlElmCar = GetNextElement(xmlElmCar)
94Wend

 1Private Function GetFirstElement _
 2  (ByVal xmlParent As IXMLDOMNode) As IXMLDOMElement
 3   If xmlParent Is Nothing Then
 4    Set GetFirstElement = Nothing
 5  ElseIf xmlParent.firstChild Is Nothing Then
 6    Set GetFirstElement = Nothing
 7  ElseIf TypeOf xmlParent.firstChild Is IXMLDOMElement Then
 8    Set GetFirstElement = xmlParent.firstChild
 9  Else
10    Set GetFirstElement = _
11      GetNextElement(xmlParent.firstChild)
12  End If
13End Function
14
15Private Function GetNextElement _
16  (ByVal xmlNode As IXMLDOMNode) As IXMLDOMElement
17  While (xmlNode Is Nothing) = False
18    Set xmlNode = xmlNode.nextSibling
19    If (xmlNode Is Nothing) = False Then
20      If TypeOf xmlNode Is IXMLDOMElement Then
21        Set GetNextElement = xmlNode
22        Exit Function
23      End If
24    End If
25  Wend
26End Function
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
 2p.Name_ = "Fred"
 3p.DateOfBirth.SetDate 1978, 6, 26
 4
 5Dim a As New Address
 6Set p.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.Print p.ToXml
21
 1Dim xmlDoc As New MSXML2.DOMDocument
 2
 3Dim xmlElmPerson As MSXML2.IXMLDOMElement
 4Set xmlElmPerson = xmlDoc.createElement("Person")
 5xmlDoc.AppendChild xmlElmPerson
 6
 7Dim xmlElmName As MSXML2.IXMLDOMElement
 8Set xmlElmName = xmlDoc.createElement("Name")
 9xmlElmPerson.AppendChild xmlElmName
10xmlElmName.Text = "Fred"
11
12Dim xmlElmDOB As MSXML2.IXMLDOMElement
13Set xmlElmDOB = xmlDoc.createElement("DateOfBirth")
14xmlElmPerson.AppendChild xmlElmDOB
15xmlElmDOB.Text = "1978-06-26"
16
17Dim xmlElmAddress As MSXML2.IXMLDOMElement
18Set xmlElmAddress = xmlDoc.createElement("Address")
19xmlElmPerson.AppendChild xmlElmAddress
20
21Dim xmlElmHouseNo As MSXML2.IXMLDOMElement
22Set xmlElmHouseNo = xmlDoc.createElement("HouseNo")
23xmlElmAddress.AppendChild xmlElmHouseNo
24xmlElmHouseNo.Text = "7"
25
26Dim xmlElmPostCode As MSXML2.IXMLDOMElement
27Set xmlElmPostCode = xmlDoc.createElement("PostCode")
28xmlElmAddress.AppendChild xmlElmPostCode
29xmlElmPostCode.Text = "WV6 6JY"
30
31Dim xmlElmCarRA As MSXML2.IXMLDOMElement
32Set xmlElmCarRA = xmlDoc.createElement("Car")
33xmlElmPerson.AppendChild xmlElmCarRA
34
35Dim xmlElmRAModel As MSXML2.IXMLDOMElement
36Set xmlElmRAModel = xmlDoc.createElement("Model")
37xmlElmCarRA.AppendChild xmlElmRAModel
38xmlElmRAModel.Text = "Ford"
39
40Dim xmlElmRAMake As MSXML2.IXMLDOMElement
41Set xmlElmRAMake = xmlDoc.createElement("Make")
42xmlElmCarRA.AppendChild xmlElmRAMake
43xmlElmRAMake.Text = "Escort"
44
45Dim xmlElmCarToy As MSXML2.IXMLDOMElement
46Set xmlElmCarToy = xmlDoc.createElement("Car")
47xmlElmPerson.AppendChild xmlElmCarToy
48
49Dim xmlElmToyModel As MSXML2.IXMLDOMElement
50Set xmlElmToyModel = xmlDoc.createElement("Model")
51xmlElmCarToy.AppendChild xmlElmToyModel
52xmlElmToyModel.Text = "Lotus"
53
54Dim xmlElmToyMake As MSXML2.IXMLDOMElement
55Set xmlElmToyMake = xmlDoc.createElement("Make")
56xmlElmCarToy.AppendChild xmlElmToyMake
57xmlElmToyMake.Text = "Elise"
58
59Debug.Print xmlDoc.Xml
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