SUMMARY
This step-by step article describes how to populate a
TreeView control by using Extensible Markup Language (XML) data in Visual
Basic .NET. Because both XML and the
TreeView control represent the data in a hierarchical format, the
TreeView control is a natural choice to display XML data.
The
TreeView control has a
Nodes collection with root
TreeNode objects. Each
TreeNode in turn has its own
Nodes collection that holds more than one child
TreeNode.
NOTE: This sample uses the Document Object Model (DOM) parsing classes
of .NET to process XML.
For more information about the design of XML
in .NET Framework, see the "References" section of this
article.
back to the topRequirements
The following list outlines the recommended hardware, software,
network infrastructure, and service packs that you need:
- Microsoft Windows XP, Microsoft Windows 2000, or Microsoft
Windows NT 4.0 Service Pack 6a
- Microsoft Data Access Components 2.6 (MDAC) or
later
- Microsoft Visual Studio .NET
This article assumes that you are familiar with the following
topics:
- Visual Basic .NET syntax
- XML and the related standards
- Windows Forms
back to the topSteps to Create and Populate the TreeView Control with XML
- Paste the following XML sample code in a new text file
named "Sample.xml". This file is the sample XML data for this example:
<?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 new Windows-based application in Visual Basic
.NET. Form1 is added to the application by default.
- Drag new TreeView, Button, Label, and TextBox controls onto Form1.
- Add the following sample code as the first line in the Code
window in Form1.vb:
Imports System.Xml
- In the Form1.vb file, replace the entire code after the
"Windows Form Designer generated code" section with the following sample code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Initialize the controls and the form.
Label1.Text = "File Path"
Label1.SetBounds(8, 8, 50, 20)
TextBox1.Text = Application.StartupPath() & "\Sample.xml"
TextBox1.SetBounds(64, 8, 256, 20)
Button1.Text = "Populate the TreeView with XML"
Button1.SetBounds(8, 40, 200, 20)
Me.Text = "TreeView control from XML"
Me.Width = 336
Me.Height = 368
TreeView1.SetBounds(8, 72, 312, 264)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
' SECTION 1. Create a DOM Document and load the XML data into it.
Dim dom As New XmlDocument()
dom.Load(TextBox1.Text)
' SECTION 2. Initialize the treeview control.
TreeView1.Nodes.Clear()
TreeView1.Nodes.Add(New TreeNode(dom.DocumentElement.Name))
Dim tNode As New TreeNode()
tNode = TreeView1.Nodes(0)
' SECTION 3. Populate the TreeView with the DOM nodes.
AddNode(dom.DocumentElement, tNode)
TreeView1.ExpandAll()
Catch xmlEx As XmlException
MessageBox.Show(xmlEx.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
Dim xNode As XmlNode
Dim tNode As TreeNode
Dim nodeList As XmlNodeList
Dim i As Long
' Loop through the XML nodes until the leaf is reached.
' Add the nodes to the TreeView during the looping process.
If inXmlNode.HasChildNodes() Then
nodeList = inXmlNode.ChildNodes
For i = 0 To nodeList.Count - 1
xNode = inXmlNode.ChildNodes(i)
inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
tNode = inTreeNode.Nodes(i)
AddNode(xNode, tNode)
Next
Else
' Here you need to pull the data from the XmlNode based on the
' type of node, whether attribute values are required, and so forth.
inTreeNode.Text = (inXmlNode.OuterXml).Trim
End If
End Sub
- Press F5 to build and run the application. Make sure that
the XML file path is correct and then click the button. The XML data should appear in the TreeView control.
NOTE: The resource could be a file, a URL or an XML Stream. Refer to
the "References" section of this article for information about using the
XmlDocument class to load XML data from different resources.
back to the topSteps to Populate the TreeView Control with Required Data
The previous code sample maps the XML tree data directly onto the
TreeView and displays all the data. Alternatively, you can add extra
information to the display or skip unwanted data.
In many instances,
you may want to display only part of the XML data. The portion of the data that
you want to display may be dynamically constructed, the result of an Extensible
Stylesheet Language (XSL) transformation, or the result of an XPath query. This
section describes how to build a new XML document with only the required nodes
and then add the new document to the
TreeView control.
For example, the following steps retrieve
only the child elements of the originial XML data by using XPath query, and
then add this list as a new node to the
TreeView.
- Paste following code just before the TreeView1.ExpandAll line in the previous sample:
' SECTION 4. Create a new TreeView Node with only the child nodes.
Dim nodelist As XmlNodeList = dom.SelectNodes("//child")
Dim cDom As New XmlDocument()
cDom.LoadXml("<children></children>")
Dim node As XmlNode
For Each node In nodelist
Dim newElem As XmlNode = cDom.CreateNode(XmlNodeType.Element, node.Name, node.LocalName)
newElem.InnerText = node.InnerText
cDom.DocumentElement.AppendChild(newElem)
Next
TreeView1.Nodes.Add(New TreeNode(cDom.DocumentElement.Name))
tNode = TreeView1.Nodes(1)
AddNode(cDom.DocumentElement, tNode)
- Build and then run the application. This application should
display a new "children" root node in the TreeView, in addition to the original data.
back to the topREFERENCES
For additional information,
refer to the following Knowledge Base article and Microsoft .NET Software
Development Kit (SDK) documentation:
313651 Roadmap for XML in the .NET Framework
back to the top