Imports System.Xml
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents trvItems As System.Windows.Forms.TreeView
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.trvItems = New System.Windows.Forms.TreeView
Me.SuspendLayout()
'
'trvItems
'
Me.trvItems.CheckBoxes = True
Me.trvItems.Dock = System.Windows.Forms.DockStyle.Fill
Me.trvItems.Location = New System.Drawing.Point(0, 0)
Me.trvItems.Name = "trvItems"
Me.trvItems.Size = New System.Drawing.Size(292, 309)
Me.trvItems.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 309)
Me.Controls.Add(Me.trvItems)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim file_name As String = Application.StartupPath
file_name = file_name.Substring(0, file_name.LastIndexOf("\")) & "\test.xml"
LoadTreeViewFromXmlFile(file_name, trvItems)
End Sub
' Load a TreeView control from an XML file.
Private Sub LoadTreeViewFromXmlFile(ByVal file_name As String, ByVal trv As TreeView)
' Load the XML document.
Dim xml_doc As New XmlDocument
xml_doc.Load(file_name)
' Add the root node's children to the TreeView.
trv.Nodes.Clear()
AddTreeViewChildNodes(trv.Nodes, xml_doc.DocumentElement)
End Sub
' Add the children of this XML node
' to this child nodes collection.
Private Sub AddTreeViewChildNodes(ByVal parent_nodes As TreeNodeCollection, ByVal xml_node As XmlNode)
For Each child_node As XmlNode In xml_node.ChildNodes
' Make the new TreeView node.
Dim new_node As TreeNode = parent_nodes.Add(child_node.Name)
' Recursively make this node's descendants.
AddTreeViewChildNodes(new_node.Nodes, child_node)
' If this is a leaf node, make sure it's visible.
If new_node.Nodes.Count = 0 Then new_node.EnsureVisible()
Next child_node
End Sub
Private Sub trvItems_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvItems.AfterSelect
End Sub
End Class