VB.Net/XML/XML Tree
Use Tree to Display XML document
<source lang="vbnet">
" ************************************************************* " * (C) Copyright 2003 by Deitel & Associates, Inc. * " * and Prentice Hall. * " * All Rights Reserved. * " * * " * DISCLAIMER: The authors and publisher of this book have * " * used their best efforts in preparing the book. These * " * efforts include the development, research, and testing of * " * the theories and programs to determine their * " * effectiveness. The authors and publisher make no warranty * " * of any kind, expressed or implied, with regard to these * " * programs or to the documentation contained in these books.* " * The authors and publisher shall not be liable in any event* " * for incidental or consequential damages in connection * " * with, or arising out of, the furnishing, performance, or * " * use of these programs. * " *************************************************************
Imports System.Xml Imports System.Windows.Forms Imports System.CodeDom.rupiler Public Class MainClass
Shared Sub Main() Dim form1 As Form = New FrmXmlDom() Application.Run(form1) End Sub " Main
End Class
Public Class FrmXmlDom
Inherits Form " TextBox and TreeView for displaying data Friend WithEvents txtConsole As TextBox Friend WithEvents treXml As TreeView " Buttons for building, printing and reseting DOM tree Friend WithEvents cmdBuild As Button Friend WithEvents cmdPrint As Button Friend WithEvents cmdReset As Button Private source As XmlDocument " reference to "XML document" " reference copy of source"s "XML document" Private copy As XmlDocument Private tree As TreeNode " TreeNode reference Public Sub New() MyBase.New() " This call is required by the Windows Form Designer. InitializeComponent() " Add any initialization after the " InitializeComponent() call " create XmlDocument and load letter.xml source = New XmlDocument() source.Load("ExampleCode.xml") " initialize references to Nothing copy = Nothing tree = Nothing End Sub " New
- Region " Windows Form Designer generated code "
"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.ruponentModel.Container "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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.treXml = New System.Windows.Forms.TreeView() Me.cmdBuild = New System.Windows.Forms.Button() Me.txtConsole = New System.Windows.Forms.TextBox() Me.cmdPrint = New System.Windows.Forms.Button() Me.cmdReset = New System.Windows.Forms.Button() Me.SuspendLayout() " "treXml " Me.treXml.ImageIndex = -1 Me.treXml.Location = New System.Drawing.Point(8, 40) Me.treXml.Name = "treXml" Me.treXml.SelectedImageIndex = -1 Me.treXml.Size = New System.Drawing.Size(344, 192) Me.treXml.TabIndex = 3 " "cmdBuild " Me.cmdBuild.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdBuild.Location = New System.Drawing.Point(8, 8) Me.cmdBuild.Name = "cmdBuild" Me.cmdBuild.Size = New System.Drawing.Size(104, 23) Me.cmdBuild.TabIndex = 0 Me.cmdBuild.Text = "Build" " "txtConsole " Me.txtConsole.Location = New System.Drawing.Point(8, 240) Me.txtConsole.Multiline = True Me.txtConsole.Name = "txtConsole" Me.txtConsole.ScrollBars = System.Windows.Forms.ScrollBars.Vertical Me.txtConsole.Size = New System.Drawing.Size(344, 96) Me.txtConsole.TabIndex = 4 Me.txtConsole.Text = "" " "cmdPrint " Me.cmdPrint.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdPrint.Location = New System.Drawing.Point(128, 8) Me.cmdPrint.Name = "cmdPrint" Me.cmdPrint.Size = New System.Drawing.Size(104, 23) Me.cmdPrint.TabIndex = 1 Me.cmdPrint.Text = "Print" " "cmdReset " Me.cmdReset.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.cmdReset.Location = New System.Drawing.Point(248, 8) Me.cmdReset.Name = "cmdReset" Me.cmdReset.Size = New System.Drawing.Size(104, 23) Me.cmdReset.TabIndex = 2 Me.cmdReset.Text = "Reset" " "FrmXmlDom " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(360, 341) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtConsole, Me.treXml, Me.cmdReset, Me.cmdPrint, Me.cmdBuild}) Me.Name = "FrmXmlDom" Me.Text = "Xml Dom" Me.ResumeLayout(False) End Sub
- End Region
Private Sub cmdBuild_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdBuild.Click If Not copy Is Nothing Then Return " document already exists End If copy = New XmlDocument() tree = New TreeNode() tree.Text = source.Name " assigns #root treXml.Nodes.Add(tree) BuildTree(source, copy, tree) End Sub Private Sub cmdPrint_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdPrint.Click If copy Is Nothing Then Return End If Dim file As TempFileCollection = New TempFileCollection() file.AddExtension("xml", False) Dim filename As String() = New String(0) {} file.CopyTo(filename, 0) Dim writer As XmlTextWriter = _ New XmlTextWriter(filename(0), _ System.Text.Encoding.UTF8) copy.WriteTo(writer) writer.Close() Dim reader As XmlTextReader = _ New XmlTextReader(filename(0)) While reader.Read If reader.NodeType = XmlNodeType.EndElement Then txtConsole.Text &= "/" End If If reader.Name <> String.Empty Then txtConsole.Text &= reader.Name & vbCrLf End If If reader.Value <> String.Empty Then txtConsole.Text &= vbTab & reader.Value & vbCrLf End If End While reader.Close() End Sub Private Sub cmdReset_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdReset.Click If Not tree Is Nothing Then treXml.Nodes.Remove(tree) End If treXml.Refresh() copy = Nothing tree = Nothing txtConsole.Clear() End Sub Private Sub BuildTree(ByVal xmlSourceNode As XmlNode, _ ByVal documentValue As XmlNode, _ ByVal treeNode As TreeNode) Dim nodeReader As XmlNodeReader = _ New XmlNodeReader(xmlSourceNode) Dim currentNode As XmlNode = Nothing Dim newNode As TreeNode = New TreeNode() Dim modifiedNodeType As XmlNodeType While nodeReader.Read modifiedNodeType = nodeReader.NodeType If modifiedNodeType = XmlNodeType.EndElement Then modifiedNodeType = XmlNodeType.Element End If currentNode = copy.CreateNode(modifiedNodeType, _ nodeReader.Name, nodeReader.NamespaceURI) Select Case nodeReader.NodeType Case XmlNodeType.Text newNode.Text = nodeReader.Value treeNode.Nodes.Add(newNode) CType(currentNode, XmlText).AppendData _ (nodeReader.Value) documentValue.AppendChild(currentNode) Case XmlNodeType.EndElement documentValue = documentValue.ParentNode treeNode = treeNode.Parent Case XmlNodeType.Element If Not nodeReader.IsEmptyElement Then newNode.Text = nodeReader.Name treeNode.Nodes.Add(newNode) treeNode = newNode documentValue.AppendChild(currentNode) documentValue = documentValue.LastChild Else newNode.Text = nodeReader.NodeType.ToString treeNode.Nodes.Add(newNode) documentValue.AppendChild(currentNode) End If Case Else newNode.Text = nodeReader.NodeType.ToString treeNode.Nodes.Add(newNode) documentValue.AppendChild(currentNode) End Select newNode = New TreeNode() End While treXml.ExpandAll() treXml.Refresh() End Sub
End Class
</source>