Click here to Skip to main content
15,879,490 members
Articles / Programming Languages / XML

Parsing XML file in VB.NET using DOM

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
12 Jun 2011CPOL1 min read 41.7K   1.3K   11  
Parsing XML file in VB.NET using DOM

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:

Image 1

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.

VB.NET
'---1.Load an xml document into a DOM instance----------------
  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)
   'MsgBox "Error: failed to load XML data from file. Check the file name."
  End If

  '-----2.Get the root node and print its name---------------------
  'get root element:
  root = oXMLDom.documentElement
  'output the name of root node

  strout = "Root node: " & root.nodeName & vbCrLf & vbCrLf

  txtOutput.Text = strout
  '----------------
  'Create an IXMLDOMNodeList object by using the xml document's
  'getElementsByTagName method, and examine the content of each node.

  '------3.Output the elements with values, parent nodes and attributes---------------
  'output "Items" nodes:
  objNodeList = oXMLDom.getElementsByTagName(" ")
  'strout = "The node list has " & objNodeList.length & " items." & vbCrLf
  Dim Attrib As Object
  '"objNodeList" index starts from 0:
  For i = 1 To (objNodeList.length)

   'Output only elements:

   If objNodeList.item(i - 1).nodeType = MSXML2.tagDOMNodeType.NODE_ELEMENT Then
    n = n + 1 'element node's counter

    '"item" index starts from 0
       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:

Image 2

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --