The ideal starting point is to show the code you have that is loading the TreeView.
Ok:
Code:
Private Sub PopulateTreeView(ByVal FileName As String)
Try
' Create a new instance of the XmlDocument class
Dim xDoc As XmlDocument = New XmlDocument
' Load XML Document
xDoc.Load(FileName)
' Clear out TreeView and add first (root) node
ConfigTreeView.Nodes.Clear()
ConfigTreeView.Nodes.Add(New TreeNode(xDoc.DocumentElement.Name))
' Create a new instance of the TreeNode class
Dim tNode As TreeNode = New TreeNode()
' Set tNode equal to the first node in the tree
tNode = ConfigTreeView.Nodes(0)
' Call AddTreeNode where I will add all nodes to the Tree
AddTreeNode(xDoc.DocumentElement, tNode)
' Expand TreeView to show all nodes (probably want to take this out when finished)
'ConfigTreeView.ExpandAll()
' xml exception
Catch xmlEx As XmlException
MessageBox.Show(xmlEx.Message)
' general exception
Catch Ex As Exception
MessageBox.Show(Ex.Message)
End Try
End Sub
' This method is called recursively until all nodes are loaded
Private Sub AddTreeNode(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)
Dim xNode As XmlNode
Dim tNode As TreeNode
Dim XNodeList As XmlNodeList
' If the current node has children, add those nodes as well
If (xmlNode.HasChildNodes) Then
XNodeList = xmlNode.ChildNodes
For x As Integer = 0 To XNodeList.Count - 1
xNode = xmlNode.ChildNodes(x)
treeNode.Nodes.Add(New TreeNode(xNode.Name))
tNode = treeNode.Nodes(x)
AddTreeNode(xNode, tNode) ' Recursive call. Keep calling self all nodes are loaded
Next
' Otherwise there are no children so add the outer xml, trimming off whitespace
Else
treeNode.Text = xmlNode.OuterXml.Trim()
End If
End Sub
The first attached image is what it would normally do, the second is what the following code will do (I did change a few things, variable names, etc...):
vb.net Code:
Private Sub PopulateTreeView(ByVal fileName As String)
Try
Dim document As XmlDocument = New XmlDocument()
document.Load(fileName)
Dim rootNode As New TreeNode(document.DocumentElement.Name)
' Clear out TreeView and add first (root) node
Me.ConfigTreeView.Nodes.Clear()
Me.ConfigTreeView.Nodes.Add(rootNode)
' Call AddTreeNode where I will add all nodes to the Tree
The first attached image is what it would normally do, the second is what the following code will do (I did change a few things, variable names, etc...):
vb.net Code:
Private Sub PopulateTreeView(ByVal fileName As String)
Try
Dim document As XmlDocument = New XmlDocument()
document.Load(fileName)
Dim rootNode As New TreeNode(document.DocumentElement.Name)
' Clear out TreeView and add first (root) node
Me.ConfigTreeView.Nodes.Clear()
Me.ConfigTreeView.Nodes.Add(rootNode)
' Call AddTreeNode where I will add all nodes to the Tree