Blue Theme Orange Theme Green Theme Red Theme
 
ASP.NET Web Hosting – Click Here
Home | Forums | Videos | Photos | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
LeftbarAd
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML .NET » XML Pathfinder - Sample C# Utility to Study XML Paths and XPath Queries

XML Pathfinder - Sample C# Utility to Study XML Paths and XPath Queries

This article discusses the construction of a simple utility that may be used to locate and evaluate paths within an XML document, and to test queries against those paths.

Author Rank:
Technologies: .NET 1.0/1.1, XML,Visual C# .NET
Total downloads : 587
Total page views :  13131
Rating :
 4.5/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
XmlPathfinderCS.zip
 
ArticleAd
Become a Sponsor



Introduction:

This article discusses the construction of a simple utility that may be used to locate and evaluate paths within an XML document, and to test queries against those paths. The application allows the user to open an XML document into a TreeView control for easy viewing of the structure; the user may right click on any node within the TreeView to expose a context menu that will allow the user to copy the current path to that node. The user may then open a test window up and paste the copied path into an expression text box. Once the path has been pasted into the text box, the user may test the path directly, or may edit the path and test the edit.

The project includes a collection of sample XML documents which may be used to try out the application.

To use the application, open the main form and use the file menu to open an xml document. The document will be displayed in the main form using a TreeView control. The user may click on any node in the TreeView and the path to that node as well as the node type will be displayed in the status bar at the bottom of the window.

The user may right click on any selected node and select from one of three options:

  1. Copy only the text contained in the current node.
  2. Copy the path to the node formatted as a query for that attribute value.
  3. Copy the full path exactly as it is.



Figure 1: Context Menu



Figure 2: The Main Form



Figure 3: The Test Form (Example shows use of the "Copy Full Path to Query an Attribute" context menu option)



Figure 4: Editing a path manually to test alternative paths ("/description" added to path shown in Figure 3)

The intent of the application was to provide a simple tool that may be used to test paths for XPath based queries; it is intended to simplify viewing the XML and to make it easier to identify specific paths within an XML document.

Getting Started:

In order to get started, unzip the included project and open the solution in the Visual Studio 2005 environment. In the solution explorer, you should note these files:



Figure 5: Solution Explorer

The Main Form (frmXmlPathfinder.cs).

The main form is used to open XML documents and to display them in TreeView format; form also provides the interface necessary to copy node paths and to open a test window which may be used to test XPath based queries against the XML document's content. The code is annotated and should be easy enough to follow from the descriptions provided:

If you'd care to open the code view up in the IDE you will see that the code file begins as follows:

using
System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;

The imports are primarily per the default configuration for a Windows application; the System.Xml and System.Xml.XPath library import are the only departure from the default.

Following the imports, the namespace and class are defined and a constructor added. A local string variable it declared and used to hold the path to the XML document.

namespace XmlPathfinder

{ 

    /// <summary>

    /// A simple utility to find paths in an xml document

    /// </summary>

    public partial class frmXmlPathfinder : Form

    {

        // private member variables

        private string mFilePath;  // the file of interest 

        public frmXmlPathfinder()

        {

            InitializeComponent();

 

            // init the file path as empty

            mFilePath = string.Empty;

        }

Next up is the method used to exit the application.

    /// <summary>

    /// Exit the application

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void tspExit_Click(object sender, EventArgs e)

    {

        Application.Exit();

    }

The next section of the code is used to load the XML document into the TreeView control.

    /// <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>

    private void PushToTreeView(XmlNode currentNode, TreeNodeCollection nodCollection)

    {

        TreeNode insertNode = nodCollection.Add(currentNode.Name); 

        switch (currentNode.NodeType)

        {

            case XmlNodeType.Element:

                insertNode.Text = currentNode.Name;

                insertNode.Tag = "Element";

                insertNode.ImageIndex = 1;

            break;

            case XmlNodeType.Attribute:

                insertNode.Text = "@" + currentNode.Name;

                insertNode.Tag = "Attribute";

                insertNode.ImageIndex = 2;

            break;

            case XmlNodeType.Text:

                insertNode.Text = currentNode.Value;

                insertNode.Tag = "Text";

                insertNode.ImageIndex = 3;

            break;

            case XmlNodeType.CDATA:

                insertNode.Text = currentNode.Value;

                insertNode.Tag = "CDATA";

                insertNode.ImageIndex = 4;

            break;

            case XmlNodeType.Comment:

                insertNode.Text = currentNode.Value;

                insertNode.Tag = "Comment";

                insertNode.ImageIndex = 5;

            break;

            case XmlNodeType.Entity:

                insertNode.Text = currentNode.Value;

                insertNode.Tag = "Entity";

                insertNode.ImageIndex = 6;

            break;

            case XmlNodeType.Notation:

                insertNode.Text = currentNode.Value;

                insertNode.Tag = "Notation";

                insertNode.ImageIndex = 7;

            break;

            default:

            break;

        }

        // recursive stuff

        // check the current node for attributes

        if (currentNode.Attributes != null && currentNode.Attributes.Count > 0)

        {

            // write out the attributes to the treeview

            foreach (XmlAttribute attribute in currentNode.Attributes)

            {

                PushToTreeView(attribute, insertNode.Nodes);

            }

        }

        // check the current node for child nodes

        if (currentNode.HasChildNodes)

        {

            // write out the child nodes to the treeview

            foreach (XmlNode childNode in currentNode.ChildNodes)

            {

                PushToTreeView(childNode, insertNode.Nodes);

            }

        }
    }

The next method is used to load up a new XML document into the form.

    /// <summary>

    /// Open a new xml document

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void tspOpen_Click(object sender, EventArgs e)

    {

        try

        {

            openFileDialog1.Title = "Open XML File";

            openFileDialog1.Filter = "XML Files|*.xml";

            openFileDialog1.DefaultExt = "XML";

            openFileDialog1.FileName = "";

            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName == "")

            return;

 
           // set the file path member var

            mFilePath = openFileDialog1.FileName;


 
          // Clear the treeview.

           treeXml.Nodes.Clear();

 

           // set the wait cursor

           this.Cursor = Cursors.WaitCursor;

 

           // create a new xml doc

           XmlDocument doc = new XmlDocument();

           try

           {

               // load the xml doc

               doc.Load(mFilePath);

 
               // set the form text to include

               // the file name

               this.Text = "XML Pathfinder - " + mFilePath;

 

               // return the cursor

               this.Cursor = Cursors.Default;

           }

           catch (Exception ex1)

           {

               // return the cursor

               this.Cursor = Cursors.Default;

 

               // tell a story

               MessageBox.Show(ex1.Message);

               return;

           }

 

           // open the doc into the treeview for

           // inspection

           PushToTreeView(doc, treeXml.Nodes);

 
           // restore the cursor

           this.Cursor = Cursors.Default;

       }

       catch (Exception ex2)

       {

           // snitch

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

       }

    }

The next bit of code is used display the selected node's path within the context of the XML document; the path shown is a cleaned up version of what appears in the TreeView. This code will display both the path as well as the selected node's type (e.g., Element, or Attribute).

    
/// <summary>

    /// Display the path and the node type in the

    /// status bar

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void treeXml_AfterSelect(object sender, TreeViewEventArgs e)

    {

        try

        {

            string tmp = 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 { }

    }

The next three methods are used to format the path and to copy it into the clipboard making it possible to then paste the selected path directly into the expression test text box on the test form.

    /// <summary>

    /// Copy the node text to the clipboard

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void copyTextToClipboardToolStripMenuItem_Click(object sender, EventArgs e)

    {

        string tmp = treeXml.SelectedNode.Text;

        Clipboard.SetDataObject(tmp, true);

    } 

    /// <summary>

    /// Copy the node's full path to the clipboard

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void copyFullPathToolStripMenuItem_Click(object sender, EventArgs e)

    {

        try

        {

            // clean #document off the path

            string tmp = 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

            int pos = 0;

            pos = tmp.LastIndexOf('@');

            pos = pos - 1;

 

            if (pos != 0)

            {

                tmp = tmp.Remove(pos, 1);

                tmp = tmp.Insert(pos, "[");

 

                int posSlash = treeXml.SelectedNode.FullPath.LastIndexOf('/');

 

                if (posSlash < pos)

                {

                    tmp += "='KeyValueHere']";

                }

                else

                {

                    tmp = tmp.Remove(posSlash-8,1);

                    tmp = tmp.Insert(posSlash-8, "='");

                    tmp += "']";

                }

            }

            Clipboard.SetDataObject(tmp, true);

        }

        catch

        {

            // if it fails, just select the selected node's

            // full path

            string tmp = treeXml.SelectedNode.FullPath;

            tmp = tmp.Replace("#document", "/");

 

            // put it in the clip board

            Clipboard.SetDataObject(tmp, true);

        }

    } 

    /// <summary>

    /// Copy the selected node's full path as is.

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void copyFullPathAsIsToolStripMenuItem_Click(object sender, EventArgs e)

    {

        try

        {

            // clean #document off the path

            string tmp = treeXml.SelectedNode.FullPath;

            tmp = tmp.Replace("#document", "/");

            Clipboard.SetDataObject(tmp, true);

        }

        catch

        {

            // do nothing

        }

    }

The last two methods in the main form are used to either open a help window or to open a test window.

    /// <summary>

    /// Open up a test window and pass in the current

    /// xml file

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void openTestWindowToolStripMenuItem_Click(object sender, EventArgs e)

    {

        if (mFilePath != string.Empty)

        {

            frmTest f = new frmTest(mFilePath);

            f.Show();

        }

        else

        {

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

        }

    } 

    
   
/// <summary>

    /// Show the help file

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void helpToolStripMenuItem1_Click(object sender, EventArgs e)

    {

        frmHelp f = new frmHelp();

        f.ShowDialog();

    }

The Text Form (frmTest.cs).

The test form is used to test paths within open XML documents and to display of any XPath type queries executed against the XML document.

If you'd care to open the code view up in the IDE you will see that the code file begins as follows:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.Xml.XPath;

The imports are primarily per the default configuration for a Windows application; the System.Xml and System.Xml.XPath library import are the only departure from the default.

Following the imports, the namespace and class are defined and a constructor added. A local string variable it declared and used to hold the path to the XML document.

namespace XmlPathfinder

{

    public partial class frmTest : Form

    {

        // the document opened for examination

        XmlDocument doc = new XmlDocument();

 

        public frmTest(string filePath)

        {

            InitializeComponent();   

 

            // load the file upon init

            try

            {

                doc.Load(filePath);

                this.Text = "Testing - " + filePath;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "Open File Error");

                return;

            }

        }

 

The next bit of code is the button click event handler for the test itself. This code uses the expression text box content as path; whatever is captured from the resulting query is displayed in the result section of the form. This is accomplished by creating an XPathNavigator and using the XML document's CreateNavigator method. Once the navigator is declared, we can send the expression search term directly to the navigator's select method to test it.

        /// <summary>

        /// Use XPath to search for the path/values

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnTest_Click(object sender, EventArgs e)

        {

            rtbResults.Text = string.Empty;

 

            // get an xpath navigator  

            XPathNavigator navigator = doc.CreateNavigator();

 

            // contain the results in a stringbuilder

            StringBuilder sb = new StringBuilder(); 

 

            try

            {

                // look for the path and use iterator to get the results

                XPathNodeIterator nodes = navigator.Select(txtTestExpression.Text);

               

                while (nodes.MoveNext())

                {

                    XPathNavigator node = nodes.Current;

 

                    // depending upon which radio button is checked,

                    // write the results to the string builder

                    if (optInnerXml.Checked == true)

                    {

                        sb.Append(node.InnerXml + Environment.NewLine);

                    }

                    else if (optOuterXml.Checked == true)

                    {

                        sb.Append(node.OuterXml + Environment.NewLine);

                    }

                    else

                    {

                        sb.Append(node.Value + Environment.NewLine);

                    }                   

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message, "XPath Error");

            }

 

            // post any results to the results box

            rtbResults.Text = sb.ToString();

        }

The last bit of the class is used to close the form.

        /// <summary>

        /// close the form, not the application

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnClose_Click(object sender, EventArgs e)

        {

            this.Dispose();

        }

The only remaining forms are used to display the help. The help file contains access to an RTF document which contains instructions and samples.

Summary

This application was provided as a starter utility application that may be used to evaluate paths when writing XPath based queries against an existing XML document.


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Boost the performance of your .NET applications
“ANTS Profiler took us straight to the specific areas of our code which were the cause of our performance issues." Terry Phillips, Sr. Developer, Harley-Davidson Dealer Systems. Download your free trial of ANTS Profiler.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
Download Files:
XmlPathfinderCS.zip
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
ArticleAd
Become a Sponsor
Latest Comments:
Subject Posted By Posted On

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved