Mega Code Archive

 
Categories / VB.Net Tutorial / XML
 

XML path navigator

'Visual Basic.NET How to Program, Second Edition 'by Harvey M. Deitel (Author), Paul J. Deitel (Author), Tem R. Nieto (Author) ' Publisher: Prentice Hall; 2 edition (December 11, 2001) ' Language: English ' ISBN-10: 0130293636 ' ISBN-13: 978-0130293633 Imports System.Windows.Forms Imports System.Xml.XPath  public class FrmPathNavigatorDemo    public Shared Sub Main         Application.Run(New FrmPathNavigator)    End Sub End class Public Class FrmPathNavigator    Inherits Form    ' GroupBox contains Controls for locating XML file    Friend WithEvents locateGroupBox As GroupBox    Friend WithEvents cmdSelect As Button    Friend WithEvents cboSelect As ComboBox    Friend WithEvents txtSelect As TextBox    ' GroupBox contains Controls for navigating DOM tree    Friend WithEvents navigateGroupBox As GroupBox    Friend WithEvents cmdNext As Button    Friend WithEvents cmdPrevious As Button    Friend WithEvents cmdParent As Button    Friend WithEvents cmdFirstChild As Button    ' TreeView displays DOM-tree results    Friend WithEvents trePath As TreeView    ' navigator to traverse document    Private xPath As XPathNavigator    ' references document for use by XPathNavigator    Private document As XPathDocument    ' references TreeNode list used by TreeView control    Private tree As TreeNode    Public Sub New()       MyBase.New()       ' This call is required by the Windows Form Designer.       InitializeComponent()       ' Add any initialization after the        ' InitializeComponent() call       ' load in XML document       document = New XPathDocument("YourFile.xml")       ' create nagivator       xPath = document.CreateNavigator       ' create root node for TreeNodes       tree = New TreeNode()       tree.Text = xPath.NodeType.ToString ' #root       trePath.Nodes.Add(tree)             ' add tree       ' update TreeView control       trePath.ExpandAll()       trePath.Refresh()       trePath.SelectedNode = tree         ' highlight root    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.ComponentModel.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.cboSelect = New System.Windows.Forms.ComboBox()       Me.trePath = New System.Windows.Forms.TreeView()       Me.navigateGroupBox = New System.Windows.Forms.GroupBox()       Me.cmdFirstChild = New System.Windows.Forms.Button()       Me.cmdParent = New System.Windows.Forms.Button()       Me.cmdNext = New System.Windows.Forms.Button()       Me.cmdPrevious = New System.Windows.Forms.Button()       Me.txtSelect = New System.Windows.Forms.TextBox()       Me.locateGroupBox = New System.Windows.Forms.GroupBox()       Me.cmdSelect = New System.Windows.Forms.Button()       Me.navigateGroupBox.SuspendLayout()       Me.locateGroupBox.SuspendLayout()       Me.SuspendLayout()       '       'cboSelect       '       Me.cboSelect.DropDownWidth = 121       Me.cboSelect.Items.AddRange(New Object() {"/sports", "/sports/game/name", "/sports/game/paragraph", "/sports/game [name='Cricket']"})       Me.cboSelect.Location = New System.Drawing.Point(104, 32)       Me.cboSelect.Name = "cboSelect"       Me.cboSelect.Size = New System.Drawing.Size(224, 24)       Me.cboSelect.TabIndex = 1       '       'trePath       '       Me.trePath.FullRowSelect = True       Me.trePath.HideSelection = False       Me.trePath.ImageIndex = -1       Me.trePath.Location = New System.Drawing.Point(8, 272)       Me.trePath.Name = "trePath"       Me.trePath.SelectedImageIndex = -1       Me.trePath.Size = New System.Drawing.Size(336, 136)       Me.trePath.TabIndex = 2       '       'navigateGroupBox       '       Me.navigateGroupBox.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdFirstChild, Me.cmdParent, Me.cmdNext, Me.cmdPrevious})       Me.navigateGroupBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))       Me.navigateGroupBox.Location = New System.Drawing.Point(84, 144)       Me.navigateGroupBox.Name = "navigateGroupBox"       Me.navigateGroupBox.Size = New System.Drawing.Size(184, 120)       Me.navigateGroupBox.TabIndex = 1       Me.navigateGroupBox.TabStop = False       Me.navigateGroupBox.Text = "Navigation Controls"       '       'cmdFirstChild       '       Me.cmdFirstChild.Location = New System.Drawing.Point(104, 56)       Me.cmdFirstChild.Name = "cmdFirstChild"       Me.cmdFirstChild.TabIndex = 5       Me.cmdFirstChild.Text = "First Child"       '       'cmdParent       '       Me.cmdParent.Location = New System.Drawing.Point(8, 56)       Me.cmdParent.Name = "cmdParent"       Me.cmdParent.TabIndex = 4       Me.cmdParent.Text = "Parent"       '       'cmdNext       '       Me.cmdNext.Location = New System.Drawing.Point(56, 88)       Me.cmdNext.Name = "cmdNext"       Me.cmdNext.TabIndex = 3       Me.cmdNext.Text = "Next"       '       'cmdPrevious       '       Me.cmdPrevious.Location = New System.Drawing.Point(56, 24)       Me.cmdPrevious.Name = "cmdPrevious"       Me.cmdPrevious.TabIndex = 2       Me.cmdPrevious.Text = "Previous"       '       'txtSelect       '       Me.txtSelect.BackColor = System.Drawing.SystemColors.Info       Me.txtSelect.Location = New System.Drawing.Point(8, 64)       Me.txtSelect.Multiline = True       Me.txtSelect.Name = "txtSelect"       Me.txtSelect.ReadOnly = True       Me.txtSelect.Size = New System.Drawing.Size(320, 56)       Me.txtSelect.TabIndex = 2       Me.txtSelect.Text = ""       '       'locateGroupBox       '       Me.locateGroupBox.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtSelect, Me.cmdSelect, Me.cboSelect})       Me.locateGroupBox.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))       Me.locateGroupBox.Location = New System.Drawing.Point(8, 8)       Me.locateGroupBox.Name = "locateGroupBox"       Me.locateGroupBox.Size = New System.Drawing.Size(336, 128)       Me.locateGroupBox.TabIndex = 0       Me.locateGroupBox.TabStop = False       Me.locateGroupBox.Text = "Locate Element"       '       'cmdSelect       '       Me.cmdSelect.Location = New System.Drawing.Point(8, 32)       Me.cmdSelect.Name = "cmdSelect"       Me.cmdSelect.TabIndex = 1       Me.cmdSelect.Text = "Select"       '       'FrmPathNavigator       '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)       Me.ClientSize = New System.Drawing.Size(352, 413)       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.trePath, Me.navigateGroupBox, Me.locateGroupBox})       Me.Name = "FrmPathNavigator"       Me.Text = "Path Navigator"       Me.navigateGroupBox.ResumeLayout(False)       Me.locateGroupBox.ResumeLayout(False)       Me.ResumeLayout(False)    End Sub #End Region    ' traverse to first child    Private Sub cmdFirstChild_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdFirstChild.Click       Dim newTreeNode As TreeNode       If xPath.MoveToFirstChild Then          newTreeNode = New TreeNode() ' create new node          DetermineType(newTreeNode, xPath)          tree.Nodes.Add(newTreeNode)          tree = newTreeNode ' assign tree newTreeNode          trePath.ExpandAll()          trePath.Refresh()          trePath.SelectedNode = tree       Else           Console.WriteLine("Current Node has no children.")       End If    End Sub ' cmdFirstChild_Click    Private Sub cmdParent_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdParent.Click       If xPath.MoveToParent Then          tree = tree.Parent          Dim count As Integer = tree.GetNodeCount(False)          Dim i As Integer          For i = 0 To count - 1             tree.Nodes.Remove(tree.FirstNode)          Next          trePath.ExpandAll()          trePath.Refresh()          trePath.SelectedNode = tree       Else           Console.WriteLine("Current node has no parent.", "", _             MessageBoxButtons.OK, MessageBoxIcon.Information)       End If    End Sub ' cmdParent_Click    Private Sub cmdNext_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdNext.Click       Dim newTreeNode As TreeNode = Nothing       Dim newNode As TreeNode = Nothing       If xPath.MoveToNext Then          newTreeNode = tree.Parent ' get parent node          newNode = New TreeNode() ' create new node          DetermineType(newNode, xPath)          newTreeNode.Nodes.Add(newNode)          tree = newNode          trePath.ExpandAll()          trePath.Refresh()          trePath.SelectedNode = tree       Else           Console.WriteLine("Current node is last sibling.", "", _             MessageBoxButtons.OK, MessageBoxIcon.Information)       End If    End Sub     Private Sub cmdPrevious_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdPrevious.Click       Dim parentTreeNode As TreeNode = Nothing       If xPath.MoveToPrevious Then          parentTreeNode = tree.Parent ' get parent node          ' delete current node          parentTreeNode.Nodes.Remove(tree)          ' move to previous node          tree = parentTreeNode.LastNode          ' update TreeView control          trePath.ExpandAll()          trePath.Refresh()          trePath.SelectedNode = tree       Else ' if current node has no previous siblings          MessageBox.Show("Current node is first sibling.", "", _             MessageBoxButtons.OK, MessageBoxIcon.Information)       End If    End Sub ' cmdPrevious_Click    ' process cmdSelect_Click event    Private Sub cmdSelect_Click(ByVal sender As System.Object, _       ByVal e As System.EventArgs) Handles cmdSelect.Click       Dim iterator As XPathNodeIterator ' enables node iteration       ' get specified node from ComboBox       Try          iterator = xPath.Select(cboSelect.Text)          DisplayIterator(iterator) ' print selection          ' catch invalid expressions       Catch argumentException As System.ArgumentException          MessageBox.Show(argumentException.Message, "Error", _             MessageBoxButtons.OK, MessageBoxIcon.Error)       End Try    End Sub ' cmdSelect_Click    ' print values for XPathNodeIterator    Private Sub DisplayIterator( _       ByVal iterator As XPathNodeIterator)       txtSelect.Clear()       ' prints selected node's values       While iterator.MoveNext          txtSelect.Text &= iterator.Current.Value.Trim & vbCrLf       End While    End Sub ' DisplayIterator    ' determine if TreeNode should display current node    ' name or value    Private Sub DetermineType(ByVal node As TreeNode, _       ByVal xPath As XPathNavigator)       ' determine NodeType       Select Case xPath.NodeType          Case XPathNodeType.Element ' if Element, get its name             ' get current node name, and remove whitespaces             node.Text = xPath.Name.Trim          Case Else  ' obtain node values             ' get current node value and remove whitespaces             node.Text = xPath.Value.Trim       End Select    End Sub End Class ' ************************************************************* ' * (C) Copyright 2002 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.                                    * ' *************************************************************