This is the xml I used for testing:
xml Code:
<?xml version="1.0"?> <Root> <FileTypes> <FileType>TXT</FileType> <FileType>DAT</FileType> <FileType>ZIP</FileType> <SomethingElse> <FileType>BAT</FileType> <FileType>EXE</FileType> </SomethingElse> </FileTypes> </Root>
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 Me.AddTreeNodes(document.DocumentElement, rootNode) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub ' This method is called recursively until all nodes are loaded Private Sub AddTreeNodes(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode) Dim currentNode As XmlNode = Nothing ' If the current node has children, add those nodes as well If (xmlNode.HasChildNodes) Then For index As Integer = 0 To xmlNode.ChildNodes.Count - 1 currentNode = xmlNode.ChildNodes(index) If currentNode.Name = "FileType" Then For Each child As XmlNode In currentNode.ChildNodes treeNode.Nodes.Add(child.Value) Next ElseIf Not Object.Equals(currentNode.Value, treeNode.Text) Then treeNode.Nodes.Add(New TreeNode(currentNode.Name)) Me.AddTreeNodes(currentNode, treeNode.Nodes(index)) End If Next End If End Sub
Let me know if that works...




Reply With Quote