frmXmlPathfinder.vb

 Imports System.Xml
Imports System.Xml.XPath


Public Class frmXmlPathfinder

    ' Member variables
    Private mFilePath As String


    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' init the file path as empty
        mFilePath = String.Empty

    End Sub



    ''' <summary>
    ''' Exit the Application
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tspExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tspExit.Click

        Application.Exit()

    End Sub



    ''' <summary>
    ''' Write the xml document into a treeview to make
    ''' easier to read and navigate, starting with the top
    ''' of the document, the method uses recursion to
    ''' populate the treeview with all the child nodes
    ''' </summary>
    ''' <param name="currentNode"></param>
    ''' <param name="nodCollection"></param>
    ''' <remarks></remarks>
    Private Sub PushToTreeView(ByVal currentNode As XmlNode, _
                               ByVal nodCollection As TreeNodeCollection)

        Try

       
            Dim insertNode As TreeNode = nodCollection.Add(currentNode.Name)

            Select Case (currentNode.NodeType)

                Case XmlNodeType.Element
                    insertNode.Text = currentNode.Name
                    insertNode.Tag = "Element"
                    insertNode.ImageIndex = 1
                Case XmlNodeType.Attribute
                    insertNode.Text = "@" + currentNode.Name
                    insertNode.Tag = "Attribute"
                    insertNode.ImageIndex = 2
                Case XmlNodeType.Text
                    insertNode.Text = currentNode.Value
                    insertNode.Tag = "Text"
                    insertNode.ImageIndex = 3
                Case XmlNodeType.CDATA
                    insertNode.Text = currentNode.Value
                    insertNode.Tag = "CDATA"
                    insertNode.ImageIndex = 4
                Case XmlNodeType.Comment
                    insertNode.Text = currentNode.Value
                    insertNode.Tag = "Comment"
                    insertNode.ImageIndex = 5
                Case XmlNodeType.Entity
                    insertNode.Text = currentNode.Value
                    insertNode.Tag = "Entity"
                    insertNode.ImageIndex = 6
                Case XmlNodeType.Notation
                    insertNode.Text = currentNode.Value
                    insertNode.Tag = "Notation"
                    insertNode.ImageIndex = 7
                Case Else
                    ' do nothing

            End Select

            ' Recursive stuff

            ' check the current node for attributes
            If Not currentNode.Attributes Is Nothing Then

                ' write out the attributes to the treeview
                Dim attribute As XmlAttribute
                For Each attribute In currentNode.Attributes
                    PushToTreeView(attribute, insertNode.Nodes)
                Next

            End If

            ' check the current node for child nodes
            If Not currentNode Is Nothing And currentNode.HasChildNodes Then

                ' write out the child nodes to the treeview
                Dim childNode As XmlNode
                For Each childNode In currentNode.ChildNodes
                    PushToTreeView(childNode, insertNode.Nodes)
                Next

            End If

        Catch ex As Exception

            MessageBox.Show(ex.Message, "Error Reading XML Document")

        End Try

    End Sub



    ''' <summary>
    ''' Open an xml document into the treeview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tspOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tspOpen.Click

        Try

            openFileDialog1.Title = "Open XML File"
            openFileDialog1.Filter = "XML Files|*.xml"
            openFileDialog1.DefaultExt = "XML"
            openFileDialog1.FileName = ""

            openFileDialog1.ShowDialog()

            If openFileDialog1.FileName = "" Then
                Return
            End If

            ' set the file path member var
            mFilePath = openFileDialog1.FileName

            ' Clear the treeview.
            treeXml.Nodes.Clear()

            ' set the wait cursor
            Me.Cursor = Cursors.WaitCursor

            ' create a new xml doc
            Dim doc As New XmlDocument()

            Try

                ' load the xml doc
                doc.Load(mFilePath)

                ' set the form text to include
                ' the file name
                Me.Text = "XML Pathfinder - " + mFilePath

                ' return the cursor
                Me.Cursor = Cursors.Default

            Catch ex1 As Exception

                ' return the cursor
                Me.Cursor = Cursors.Default

                ' tell a story
                MessageBox.Show(ex1.Message, "Error Opening XML File")
                Return

            End Try


            ' open the doc into the treeview for
            ' inspection
            PushToTreeView(doc, treeXml.Nodes)

            ' restore the cursor
            Me.Cursor = Cursors.Default


        Catch ex2 As Exception

            ' snitch
            MessageBox.Show(ex2.Message, "Unable to Open Document")

        End Try

    End Sub



    ''' <summary>
    ''' Update the status bar when a new node is selected
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub treeXml_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeXml.AfterSelect

        Try

            Dim tmp As String = treeXml.SelectedNode.FullPath
            tmp = tmp.Replace("#document", "/")

            ' show the path in the status bar
            statusPath.Text = "Selected Path: " + tmp

            ' show the selected node type in the status bar
            statusType.Text = "     Selected Type: " + treeXml.SelectedNode.Tag

        Catch

            ' skip this update

        End Try

    End Sub



    ''' <summary>
    ''' Copy only the selected node's text to the clipboard
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub copyTextToClipboardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copyTextToClipboardToolStripMenuItem.Click

        Dim tmp As String = treeXml.SelectedNode.Text
        Clipboard.SetDataObject(tmp, True)

    End Sub



    ''' <summary>
    ''' Copy the full path of the node and try to convert the attribute
    ''' portion into a formatted query for that value; if the user
    ''' performs this action on an invalid selection (not an
    ''' attribute, it will give a useless result)
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub copyFullPathToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copyFullPathToolStripMenuItem.Click

        Try

            ' clean #document off the path
            Dim tmp As String = treeXml.SelectedNode.FullPath
            tmp = tmp.Replace("#document", "/")

            ' check to see if there is an attribute present
            ' you'd likely query the attribute this can
            ' get hosed if the user selects this option and
            ' has not clicked on an attribute's value
            Dim pos As Integer = 0
            pos = tmp.LastIndexOf("@")
            pos = pos - 1

            If pos <> 0 Then

                tmp = tmp.Remove(pos, 1)
                tmp = tmp.Insert(pos, "[")

                Dim posSlash As Integer = treeXml.SelectedNode.FullPath.LastIndexOf("/")

                If posSlash < pos Then

                    tmp += "='KeyValueHere')"
                    End

                Else

                    tmp = tmp.Remove(posSlash - 8, 1)
                    tmp = tmp.Insert(posSlash - 8, "='")
                    tmp += "']"

                End If

            End If

                Clipboard.SetDataObject(tmp, True)

            catch

            ' if it fails, just select the selected node's
            ' full path
            Dim tmp As String = treeXml.SelectedNode.FullPath
            tmp = tmp.Replace("#document", "/")

            ' put it in the clip board
            Clipboard.SetDataObject(tmp, True)

        End Try

    End Sub



    ''' <summary>
    ''' Copy the exact path to the node without
    ''' making any modifications to it
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub copyFullPathAsIsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles copyFullPathAsIsToolStripMenuItem.Click

        Try

            ' clean #document off the path
            Dim tmp As String = treeXml.SelectedNode.FullPath
            tmp = tmp.Replace("#document", "/")

            Clipboard.SetDataObject(tmp, True)

        Catch

            ' do nothing

        End Try

    End Sub



    ''' <summary>
    ''' Open the test file if there is an active
    ''' xml file open in the main form
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tspOpenTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tspOpenTest.Click

        If mFilePath <> String.Empty Then

            Dim f As New frmTest(mFilePath)
            f.Show()

        Else

            MessageBox.Show("Open an xml document prior to starting a test.", "Invalid File")

        End If

    End Sub



    ''' <summary>
    ''' Open the help file
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tspOpenHelp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tspOpenHelp.Click

        Dim f As New frmHelp()
        f.Show()

    End Sub


End Class

Project Homepage: