Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 244954 - Last Review: July 1, 2004 - Revision: 3.1
How To Populate a Treeview Control with an XML File
This article was previously published under Q244954
On This Page
SUMMARY
For a Microsoft Visual Basic .NET version of this
article, see
308063Â
(http://kbalertz.com/Feedback.aspx?kbNumber=308063/EN-US/
)
.
This article illustrates how to populate a
Treeview control from an XML file in Visual Basic.
MORE INFORMATION
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Â
(http://kbalertz.com/Feedback.aspx?kbNumber=305019/EN-US/
)
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:
<?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>
- 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
- Run the project.
- Type the path to Sample.xml, and then click the command
button.
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 Parser 3.0
| kbhowto kbtreeview KB244954 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please