Microsoft KB Archive/308063

= How To Populate a TreeView Control with XML Data in Visual Basic .NET =

Article ID: 308063

Article Last Modified on 7/15/2004

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.0
 * Microsoft .NET Framework 1.1

-



This article was previously published under Q308063





For a Microsoft Visual C# .NET version of this article, see 317597.



For a Microsoft Visual Basic 6.0 version of this article, see 244954.

IN THIS TASK
SUMMARY
 * Requirements
 * Steps to Create and Populate the TreeView Control with XML
 * Steps to Populate the TreeView Control with Required Data

REFERENCES



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 &quot;References&quot; section of this article.

back to the top

Requirements
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 top

Steps to Create and Populate the TreeView Control with XML
  Paste the following XML sample code in a new text file named &quot;Sample.xml&quot;. This file is the sample XML data for this example:  id=&quot;grandfather&quot; id=&quot;father&quot; id=&quot;brother&quot; id=&quot;niece&quot; id=&quot;me&quot; id=&quot;son&quot; id=&quot;daughter&quot; id=&quot;sister&quot; id=&quot;uncle&quot; id=&quot;cousin sister&quot; id=&quot;second cousin&quot; id=&quot;cousin brother&quot;  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 &quot;Windows Form Designer generated code&quot; 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 = &quot;File Path&quot; Label1.SetBounds(8, 8, 50, 20) TextBox1.Text = Application.StartupPath & &quot;\Sample.xml&quot; TextBox1.SetBounds(64, 8, 256, 20) Button1.Text = &quot;Populate the TreeView with XML&quot; Button1.SetBounds(8, 40, 200, 20) Me.Text = &quot;TreeView control from XML&quot; 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 </li> 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.</li></ol>

NOTE: The resource could be a file, a URL or an XML Stream. Refer to the &quot;References&quot; section of this article for information about using the XmlDocument class to load XML data from different resources.

back to the top

Steps 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. <ol>  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(&quot;//child&quot;) Dim cDom As New XmlDocument cDom.LoadXml(&quot; &quot;) 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) </li> Build and then run the application. This application should display a new &quot;children&quot; root node in the TreeView, in addition to the original data.</li></ol>

back to the top