Solved

populating a treeview from a xml source

Posted on 2009-04-06
1
392 Views
Last Modified: 2013-11-08
I am really very new to asp.net. 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
 
      Try
            ' SECTION 1. Create a DOM Document and load the XML data into it.
            Dim dom As New System.Xml.XmlDocument
            dom.Load("c:\uac\questionnaire.xml")
 
            ' 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)
            ' 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()
            cDom.LoadXml("<children></children>")
            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
                cDom.DocumentElement.AppendChild(newElem)
            Next
 
            TreeView1.Nodes.Add(New TreeNode(cDom.DocumentElement.Name))
            tNode = TreeView1.Nodes(1)
            AddNode(cDom.DocumentElement, tNode)
 
            TreeView1.ExpandAll()
 
        Catch xmlEx As System.Xml.XmlException
            MsgBox(xmlEx.Message)
        Catch ex As Exception
            MsgBox(ex.Message)
        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)
            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

Open in new window

0
Comment
Question by:Anthony Matovu
1 Comment
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 24084842
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

828 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