By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,066 Members | 1,625 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,066 IT Pros & Developers. It's quick & easy.

XML issues: change name displayed in node

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

http://vb.net-informations.com/xml/v...w-from-xml.htm

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?
May 2 '13 #1
Share this Question
Share on Google+
1 Reply


P: 82
I got past the node name by using inTreeNode.Text = inXmlNode.InnerText.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.Parent Is Nothing Then
'this is top level. do nothing
Else
inTreeNode.Text = inXmlNode.InnerText.ToString
End If


Expand|Select|Wrap|Line Numbers
  1.      Label1.Text = "File Path"
  2.         Label1.SetBounds(8, 8, 50, 20)
  3.         TextBox1.Text = Application.StartupPath() & "\Sample.xml"
  4.         Dim filename As String = Application.StartupPath() & "\Sample.xml"
  5.         TextBox1.SetBounds(64, 8, 256, 20)
  6.         Button1.Text = "Populate the TreeView with XML"
  7.         Button1.SetBounds(8, 40, 200, 20)
  8.         Me.Text = "TreeView control from XML"
  9.         Me.Width = 336
  10.         Me.Height = 368
  11.         TreeView1.SetBounds(8, 72, 312, 264)
  12.  
  13.         Dim xmldoc As New XmlDataDocument()
  14.         Dim xmlnode As XmlNode
  15.         Dim fs As New FileStream("Sample.xml", FileMode.Open, FileAccess.Read)
  16.         xmldoc.Load(fs)
  17.         xmlnode = xmldoc.ChildNodes(1)
  18.         TreeView1.Nodes.Clear()
  19.         TreeView1.Nodes.Add(New TreeNode(xmldoc.DocumentElement.Name))
  20.         Dim tNode As TreeNode
  21.         tNode = TreeView1.Nodes(0)
  22.         AddNode(xmlnode, tNode)
  23.     End Sub
  24.  
  25.     Private Sub AddNode(ByVal inXmlNode As XmlNode, ByVal inTreeNode As TreeNode)
  26.         Dim xNode As XmlNode
  27.         Dim tNode As TreeNode
  28.         Dim nodeList As XmlNodeList
  29.  
  30.         Dim i As Integer
  31.         Dim j As Integer = 0
  32.         If inXmlNode.HasChildNodes Then
  33.             nodeList = inXmlNode.ChildNodes
  34.             For i = 0 To nodeList.Count - 1
  35.                 xNode = inXmlNode.ChildNodes(i)
  36.                 inTreeNode.Nodes.Add(New TreeNode(xNode.Name))
  37.                 inTreeNode.Text = inXmlNode.InnerText.ToString
  38.                 tNode = inTreeNode.Nodes(i)
  39.                 AddNode(xNode, tNode)
  40.             Next
  41.         Else
  42.             inTreeNode.Text = " "
  43.         End If
  44.     End Sub
May 3 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.