Results 1 to 2 of 2

Thread: Populate TreeView component with XML ?

  1. #1

    Thread Starter
    Member hardyvoje's Avatar
    Join Date
    Feb 2006
    Location
    Serbia
    Posts
    38

    Populate TreeView component with XML ?

    I sow that there is XMLDataSource type for Web developing but how to populate TreeView with XML file?

    There is way manualy to do it, probably, but there should be some easier way....
    something like>

    VB Code:
    1. dim XmlDoc as XMLdocument
    2. XmlDoc.Load("some.xml")
    3. me.treeview1.populate(XmlDoc)

    Thanks!!!!
    Free tutorials, web templates, images, 3D models and other design&developing resources at: http://www.omnetwork.net | Open Source Gaming Portal: www.osgamer.org

  2. #2

    Thread Starter
    Member hardyvoje's Avatar
    Join Date
    Feb 2006
    Location
    Serbia
    Posts
    38

    Re: Populate TreeView component with XML ?

    I found solution on Microsoft's web site. Here, for other searchers:

    VB Code:
    1. ' put this on Button.click procedure, or some else that should start populating
    2. TreeView1.Nodes.Clear()
    3. TreeView1.Nodes.Add(New TreeNode(XmlDoc.DocumentElement.Name))
    4. Dim tNode As New TreeNode()
    5. tNode = TreeView1.Nodes(0)
    6.  
    7. AddNode(XmlDoc.DocumentElement, tNode)
    8. TreeView1.ExpandAll()
    9.  
    10. 'and also define function that is needed
    11. Private Sub AddNode(ByRef inXmlNode As XmlNode, ByRef inTreeNode As TreeNode)
    12.         Dim xNode As XmlNode
    13.         Dim tNode As TreeNode
    14.         Dim nodeList As XmlNodeList
    15.         Dim i As Integer
    16.         ' Loop through the XML nodes until the leaf is reached.
    17.         ' Add the nodes to the TreeView during the looping process.
    18.         If inXmlNode.HasChildNodes() Then
    19.             nodeList = inXmlNode.ChildNodes
    20.             For i = 0 To nodeList.Count - 1
    21.                 xNode = inXmlNode.ChildNodes(i)
    22.                 inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
    23.                 tNode = inTreeNode.Nodes(i)
    24.  
    25.                 AddNode(xNode, tNode)
    26.             Next
    27.         Else
    28.             ' Here you need to pull the data from the XmlNode based on the
    29.             ' type of node, whether attribute values are required, and so forth.
    30.             inTreeNode.Text = (inXmlNode.OuterXml).Trim
    31.         End If
    32.     End Sub

    Original Microsoft's snippet had: Dim i As Long
    but IDE showed me some error... so i set it to Integer.

    Hope this will help to someone...
    Free tutorials, web templates, images, 3D models and other design&developing resources at: http://www.omnetwork.net | Open Source Gaming Portal: www.osgamer.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width