Microsoft KB Archive/244954

= How To Populate a Treeview Control with an XML File =

Article ID: 244954

Article Last Modified on 7/1/2004

-

APPLIES TO


 * 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 Core Services 4.0

-



This article was previously published under Q244954



SUMMARY


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.   Paste following XML code into an empty text file and save the file as Sample.xml :  id="grandfather" id="father" id="brother" id="niece" id="me" id="son" id="daughter" id="sister" id="uncle" id="cousin sister" id="second cousin" id="cousin brother"  Create a Standard EXE project in Visual Basic. Form1 is added by default. From the Project menu, choose References, and add a reference to Microsoft XML, version 2.0. From the Project menu, choose Components, and add the Microsoft Windows Common Controls to the project. Add a command button, a text box, a label, and a Treeview control to the form.  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.DOMDocument40

Private 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 Sub

Private 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 Sub

Private 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 Next End Sub </li> Run the project.</li> Type the path to Sample.xml, and then click the command button.</li></ol>

Additional query words: XML, VB, Treeview, DOM

Keywords: kbhowto kbtreeview KB244954

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.