populating a treeview from a xml source

Posted on 2009-04-06
Last Modified: 2013-11-08
I am really very new to I have a code below that I am using to populate a treeview.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            ' SECTION 1. Create a DOM Document and load the XML data into it.

            Dim dom As New System.Xml.XmlDocument


            ' SECTION 2. Initialize the treeview control.


            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)

            ' SECTION 4. Create a new TreeView Node with only the child nodes.

            Dim nodelist As System.Xml.XmlNodeList = dom.SelectNodes("//child")

            Dim cDom As New System.Xml.XmlDocument()


            Dim node As System.Xml.XmlNode

            For Each node In nodelist

                Dim newElem As System.Xml.XmlNode = cDom.CreateNode(System.Xml.XmlNodeType.Element, node.Name, node.LocalName)

                newElem.InnerText = node.InnerText



            TreeView1.Nodes.Add(New TreeNode(cDom.DocumentElement.Name))

            tNode = TreeView1.Nodes(1)

            AddNode(cDom.DocumentElement, tNode)


        Catch xmlEx As System.Xml.XmlException


        Catch ex As Exception


        End Try

    End Sub

    Private Sub AddNode(ByRef inXmlNode As System.Xml.XmlNode, ByRef inTreeNode As TreeNode)

        Dim xNode As System.Xml.XmlNode

        Dim tNode As TreeNode

        Dim nodeList As System.Xml.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.ChildNodes.Add(New TreeNode(xNode.Name))

                tNode = inTreeNode.ChildNodes(i)

                AddNode(xNode, tNode)



            ' 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

Open in new window

Question by:Anthony Matovu
1 Comment
LVL 83

Accepted Solution

CodeCruiser earned 500 total points
ID: 24084842

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction HyperText Transfer Protocol ( or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Using Quotation Marks in PHP This question ( seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule… provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now