XML issues: change name displayed in node

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • starlight849
    New Member
    • Jun 2009
    • 82

    XML issues: change name displayed in node

    Through this link is an example of importing xml into a treeview in vb.net.

    How to create a TreevView from an XML File - Reading an XML file means that we are reading the data embedded in tags in an XML file


    I have this example working fine in vb.net 2008.

    What I am having trouble with is how to change the name that displays in the node to an id that I set, or even the inner contents of the xml tags. Does anyone have any suggestions?
    Last edited by acoder; May 2 '13, 11:38 PM. Reason: Fixed URL
  • starlight849
    New Member
    • Jun 2009
    • 82

    #2
    I got past the node name by using inTreeNode.Text = inXmlNode.Inner Text.ToString
    This works, however it takes the very top node and edits the name of it to be the contents of every node below it.
    Is there a way to skip the very top node?

    If inTreeNode.Pare nt Is Nothing Then
    'this is top level. do nothing
    Else
    inTreeNode.Text = inXmlNode.Inner Text.ToString
    End If


    Code:
         Label1.Text = "File Path"
            Label1.SetBounds(8, 8, 50, 20)
            TextBox1.Text = Application.StartupPath() & "\Sample.xml"
            Dim filename As String = 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)
    
            Dim xmldoc As New XmlDataDocument()
            Dim xmlnode As XmlNode
            Dim fs As New FileStream("Sample.xml", FileMode.Open, FileAccess.Read)
            xmldoc.Load(fs)
            xmlnode = xmldoc.ChildNodes(1)
            TreeView1.Nodes.Clear()
            TreeView1.Nodes.Add(New TreeNode(xmldoc.DocumentElement.Name))
            Dim tNode As TreeNode
            tNode = TreeView1.Nodes(0)
            AddNode(xmlnode, tNode)
        End Sub
    
        Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
            Dim xNode As XmlNode
            Dim tNode As TreeNode
            Dim nodeList As XmlNodeList
    
            Dim i As Integer
            Dim j As Integer = 0
            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))
                    inTreeNode.Text = inXmlNode.InnerText.ToString
                    tNode = inTreeNode.Nodes(i)
                    AddNode(xNode, tNode)
                Next
            Else
                inTreeNode.Text = " "
            End If
        End Sub
    Last edited by starlight849; May 3 '13, 03:12 PM. Reason: Solved

    Comment

    Working...