Introduction
There are many versions of XML parsers/readers on the Internet and on CodeProject (including my own in C/C++). This one was written several years ago in VB 6.0 and is based on DOM (Document Object Model). I almost forgot about it until I recently decided to upgrade (or to see if it is possible without much hassle) VB 6.0 projects to VB 2008 (VB 2010 does not recognize VB 6.0 project’s files). I already had a very time consuming experience upgrading a project with many drawing and database related functions and arrays, because it requires you to manually rewrite the old code.
XML parser does not have any drawing functions or VB6-arrays and was upgraded to VB.NET automatically, though generating warnings related mostly to default property of the object. I removed these lines from code but for anyone interested, I left the _UpgradeReport.htm in the project.
Using the Code
XML file provided with the code sample is stocks.xml file from MSXML 4.0 SDK:
Sample XML file
Though VB 6.0 and VB.NET are different languages, they still have very much in common and for the fairness of this experimental upgrade, I didn’t change and didn’t correct anything in the code. Thus, for example, you can see old style error handling instead of Try-Catch
blocks.
oXMLDom.async = False
oXMLDom.validateOnParse = False
oXMLDom.resolveExternals = False
oXMLDom.preserveWhiteSpace = True
fileName = txtFileName.Text
If oXMLDom.load(My.Application.Info.DirectoryPath & "\" & fileName) = False Then
ErrorMessage = "Error message: failed to load XML data from file." _
& vbCrLf & "Check the file name."
Response = MsgBox(ErrorMessage, StyleWarning)
End If
root = oXMLDom.documentElement
strout = "Root node: " & root.nodeName & vbCrLf & vbCrLf
txtOutput.Text = strout
objNodeList = oXMLDom.getElementsByTagName(" ")
Dim Attrib As Object
For i = 1 To (objNodeList.length)
If objNodeList.item(i - 1).nodeType = MSXML2.tagDOMNodeType.NODE_ELEMENT Then
n = n + 1
strout = strout & vbCrLf & CStr(n) & vbCrLf & "Type: " _
& objNodeList.item(i - 1).nodeTypeString & vbCrLf & "Node: " _
& objNodeList.item(i - 1).nodeName & vbCrLf & "Text: " _
& objNodeList.item(i).text & vbCrLf & "Parent node: " _
& objNodeList.item(i - 1).parentNode.nodeName & vbCrLf _
& "Child nodes: " & objNodeList.item(i - 1).childNodes.length _
& vbCrLf & vbCrLf
For Each Attrib In objNodeList.item(i - 1).attributes
strout = strout & "Attribute type: " & Attrib.nodeTypeString & _
vbCrLf & "Attribute name: " _
& Attrib.Name & vbCrLf & "Value: " & Attrib.nodeValue & vbCrLf
Next Attrib
End If
Next
This is the output of the parsing:
Output of the parsing of stocks.xml file
Note that to run the executable, you have to place the generated Interop.MSXML2.dll file in .exe file's directory.
References
History
- 08.2009 - Built in Visual Basic 6.0
- 12.06.2011 - Recompiled in Visual Basic 2008