login | register    

XML Data Binding for Visual Basic 6.0

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 - Bookstore.xsd - Click to View
The model for Bookstore.xsd
The Bookstore.xsd schema shown graphically using Liquid XML Studio
1<?xml version="1.0" encoding="utf-16"?> 2<!--Created with Liquid XML Studio --> 3<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 4 <xs:element name="Person"> 5 <xs:complexType> 6 <xs:sequence> 7 <xs:element name="Name" type="xs:string" /> 8 <xs:element name="DateOfBirth" type="xs:date" /> 9 <xs:element minOccurs="0" name="Address"> 10 <xs:complexType> 11 <xs:sequence> 12 <xs:element name="HouseNo" type="xs:int" /> 13 <xs:element name="PostCode" type="xs:string" /> 14 </xs:sequence> 15 </xs:complexType> 16 </xs:element> 17 <xs:element minOccurs="0" maxOccurs="unbounded" name="Car"> 18 <xs:complexType> 19 <xs:sequence> 20 <xs:element name="Make" type="xs:string" /> 21 <xs:element name="Model" type="xs:string" /> 22 </xs:sequence> 23 </xs:complexType> 24 </xs:element> 25 </xs:sequence> 26 </xs:complexType> 27 </xs:element> 28</xs:schema>


Sample XML File - BookStoreSample.xml - Click to View
1<?xml version="1.0"?> 2<Person> 3 <Name>Fred</Name> 4 <DateOfBirth>1978-06-26</DateOfBirth> 5 <Address> 6 <HouseNo>7</HouseNo> 7 <PostCode>WV6 6JY</PostCode> 8 </Address> 9 <Car> 10 <Make>Escort</Make> 11 <Model>Ford</Model> 12 </Car> 13 <Car> 14 <Make>Elise</Make> 15 <Model>Lotus</Model> 16 </Car> 17</Person> 18

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.