How To Populate a Treeview Control with an XML File

This article was previously published under Q244954
This article has been archived. It is offered "as is" and will no longer be updated.
For a Microsoft Visual Basic .NET version of this article, see 308063.
This article illustrates how to populate a Treeview control from an XML file in Visual Basic.

Step by Step Example

Note The sample code provided in this article contains a reference to MSXML 2.0.

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:
305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds
If a newer version of MSXML has been installed in side-by-side mode on your machine, the code may use the older version.
  1. Paste following XML code into an empty text file and save the file as Sample.xml:
    <?xml version="1.0"?>   <family>   <parent>id="grandfather"       <parent>id="father"            <parent>id="brother"               <child>id="niece"               </child>            </parent>            <parent>id="me"               <child>id="son"</child>               <child>id="daughter"</child>            </parent>            <child>id="sister"</child>        </parent>        <parent>id="uncle"            <parent>id="cousin sister"               <child>id="second cousin"</child>            </parent>            <child>id="cousin brother"</child>        </parent>   </parent>   </family>					
  2. Create a Standard EXE project in Visual Basic. Form1 is added by default.
  3. From the Project menu, choose References, and add a reference to Microsoft XML, version 2.0.
  4. From the Project menu, choose Components, and add the Microsoft Windows Common Controls to the project.
  5. Add a command button, a text box, a label, and a Treeview control to the form.
  6. Paste the following code into Form1:
       Option Explicit   Dim XMLDoc As MSXML.DOMDocument   'To use a newer version of MSXML parser, use a declaration like the      'following with the appropriate version in the ProgID. Same applies to the    'other sections of the code:   'Dim XMLDoc As MSXML2.DOMDocument40Private Sub Form_Load()    Form1.Move 0, 0, 5565, 4495    Command1.Move 120, 600, 1335, 375    Text1.Move 1560, 120, 3735, 285    Label1.Move 120, 120, 1575, 285    TreeView1.Move 120, 1080, 5175, 2895    Command1.Caption = "Load XML File"    Label1.Caption = "Enter path:"    Text1.Text = App.Path & "\Sample.XML"    Form1.Caption = "Load XML File Sample"End SubPrivate Sub Command1_Click()    Set XMLDoc = New DOMDocument    XMLDoc.async = False    'XMLDoc.validateOnParse = False    'If validation is not important, skip it    XMLDoc.Load Text1.Text        If XMLDoc.parseError.errorCode = 0 Then        MsgBox "Succeeded"        If XMLDoc.readyState = 4 Then            TreeView1.Nodes.Clear            AddNode XMLDoc.documentElement        End If    Else        MsgBox XMLDoc.parseError.reason & vbCrLf & _        XMLDoc.parseError.Line & vbCrLf & _        XMLDoc.parseError.srcText    End If    End SubPrivate Sub AddNode(ByRef XML_Node As IXMLDOMNode, _                    Optional ByRef TreeNode As Node)    Dim xNode As Node    Dim xNodeList As IXMLDOMNodeList    Dim i As Long        If TreeNode Is Nothing Then        Set xNode = TreeView1.Nodes.Add    Else        Set xNode = TreeView1.Nodes.Add(TreeNode, tvwChild)    End If        xNode.Expanded = True    xNode.Text = XML_Node.nodeName        If xNode.Text = "#text" Then        xNode.Text = XML_Node.nodeTypedValue    Else        xNode.Text = "<" + xNode.Text + ">"    End If        Set xNodeList = XML_Node.childNodes    For i = 0 To xNodeList.length - 1        AddNode xNodeList.Item(i), xNode    NextEnd Sub					
  7. Run the project.
  8. Type the path to Sample.xml, and then click the command button.
XML, VB, Treeview, DOM

Article ID: 244954 - Last Review: 02/28/2014 00:29:13 - Revision: 3.1

Microsoft Visual Basic 5.0 Learning Edition, Microsoft Visual Basic 6.0 Learning Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 6.0 Enterprise Edition, Microsoft XML Core Services 4.0, Microsoft XML Parser 2.0, Microsoft XML Parser 2.5, Microsoft XML Parser 2.6, Microsoft XML Parser 3.0

  • kbnosurvey kbarchive kbhowto kbtreeview KB244954