Building an XPath Visualizer with Windows Forms

 

Prajakta Joshi
Microsoft Corporation

August 2004

Applies to:
   Microsoft Windows
   XML programming

Summary: Praj Joshi produces a GUI tool for viewing the results of XPath queries on an XML document. The tool is built using the XPathReader and .NET WinForms classes. (6 printed pages)

Click here to download the code sample for this article.

Contents

Introduction
Design Goals of the XPath Viewer
A First Look at XPath Viewer
Overview of Functionality
Some Limitations
Conclusion
Acknowledgements

Introduction

As a member of the XML test team I and others on my team have developed a number of tools that aid in the testing of our products. One of the tools I've found useful is one that I wrote that utilizes the XPathReader class to show the nodes in an XML document that match a user-specified XPath expression. This article describes the XPath Viewer tool complete with the associated code as a download.

Design Goals of the XPath Viewer

When I set out to write the XPath Viewer tool I set for myself the following design goals:

  1. A simple, lightweight application that can be used to test XPath expressions.
  2. Gives a simple "notepad" like view of XML as it is stored in the file, rather that a tree view (Internet Explorer style).
  3. Highlights selected XPath nodes in XML.
  4. Shows properties of each matched node in a grid view.
  5. Allows users to perform queries with namespaces.
  6. Allows users to easily add/remove namespace prefix and URI values in a provided namespace manager.
  7. The application must be able to run on a system that meets the following minimum requirements:
    • Microsoft Windows 2000, Windows XP, or above.
    • Microsoft .NET Framework 1.1.

A First Look at XPath Viewer

The viewer workspace is divided into four areas. Figure 1 below shows a snapshot of the UI.

Click here for larger image.

Figure 1. Snapshot of the UI viewer

Most of the UI is quite intuitive.

  • In the upper left panel, you can enter an XPath expression. Also, if the XML file against which you want to query is a "fragment," you should check the checkbox.
  • In the upper right "Namespaces" grid, you can edit namespace <prefix, uri> values. This datagrid gives a view over the contents of the NamespaceManger object in the XpathCollection class.
  • The lower left "XML" panel is a read-only RichTextBox control that displays the contents of the XML file you load.
  • The lower right panel shows "Selected XPathReader Nodes" in a datagrid. (Obviously, it is read-only.)

Overview of Functionality

Editing Namespaces

The XPathCollection class is used for specifying <prefix, namespace> bindings used by the XPathReader when matching nodes against XPath expressions. The "Namespaces" grid simply gives a view over the namespace manager in the XPathCollection class. This grid is implemented using the DataGrid class. Its DataSource property is set to a DataTable and the DataTable's RowDeleting and RowChanged events are used to manipulate the XPathCollection's namespace manager as follows.

private void namespaces_RowDeleting( object sender, DataRowChangeEventArgs e )
{   
   this.xpc.NamespaceManager.RemoveNamespace(e.Row[0].ToString(),
    e.Row[1].ToString());
}

private void namespaces_RowChanged( object sender, DataRowChangeEventArgs e )
{   
   this.xpc.NamespaceManager.AddNamespace(e.Row[0].ToString(),
    e.Row[1].ToString());
}

Initializing XPathReader

When a user loads an XML file, it is loaded appropriately (as a document or fragment) using the streaming XmlTextReader class. Next, an instance of XPathReader is created that takes an XmlReader and an XPathCollection object.

if (checkBoxFrag.Checked)
{
   FileStream strmTemp = new FileStream(xmlFile, FileMode.Open,
  FileAccess.Read);
   XmlParserContext parserContext = new XmlParserContext(new NameTable(),
    null, null, XmlSpace.Default);
   this.xtr = new XmlTextReader(strmTemp, XmlNodeType.Element, parserContext);   
}
else               
{
   this.xtr = new XmlTextReader(xmlFile);   
}
this.xpr = new XPathReader(xtr, xpc);
this.xpc.Clear();
this.xpc.Add(textBoxExpr.Text);

Displaying Matching Nodes

I use the RichTextBox class to display XML in a notepad-like view. It exposes a Select() method, which I can use to highlight text within the control. Here is a part of the code, which calls ReadUntilMatch() on the XPathReader to iterate through matching nodes.

while (this.xpr.ReadUntilMatch())
{   
   switch (this.xpr.NodeType)
   {
      case XmlNodeType.Element:
      case XmlNodeType.EndElement:
      case XmlNodeType.Attribute:
      case XmlNodeType.ProcessingInstruction:
         startPos = this.GetStartPosition(xtr.LineNumber, xtr.LinePosition);
         richTextBoxXml.Select(startPos, xpr.Name.Length);
         this.AddNode();
         break;

      case XmlNodeType.Comment:
      case XmlNodeType.Whitespace:
      case XmlNodeType.SignificantWhitespace:
      case XmlNodeType.Text:
         startPos = this.GetStartPosition(xtr.LineNumber, xtr.LinePosition);
         richTextBoxXml.Select(startPos, xpr.Value.Length);
         this.AddNode();
         break;
               
      case XmlNodeType.Document:
      case XmlNodeType.DocumentFragment:
      case XmlNodeType.DocumentType:
      case XmlNodeType.Entity:
      case XmlNodeType.EntityReference:
      case XmlNodeType.EndEntity:
      case XmlNodeType.Notation:
      case XmlNodeType.XmlDeclaration:
      case XmlNodeType.None:
      default:
      MessageBox.Show("XPath 1.0 data model does not define node of type "
 + xpr.NodeType, "Bug in XPathReader", MessageBoxButtons.OK);
         break;
   }
}

As you can see, some XmlNodeType values cannot be matched by an XPath expression, since the XPath 1.0 data model does not support those node types. (The XPathNavigator class in the framework uses the enum XpathNodeType instead.) Along with highlighting matching nodes, they are also displayed in the datagrid. The AddNode() method shown above adds a new row for each matching node.

Changing Reader Properties to Display

Figure 2 below shows how you can edit reader properties to be displayed in the grid. Since I do not cache any of the information about matching reader-nodes, the changes in columns take effect the next time you perform a query.

Click here for larger image.

Figure 2. Edit reader properties

Some Limitations

  • Since I use XPathReader implementation under the covers, this tool supports the same subset of XPath 1.0 as XPathReader.
  • The XPathReader class design allows users to match a current reader node against multiple XPath expressions added to the XPathCollection. The XPath Viewer does not support multiple XPath expressions.
  • To better represent selected nodes it would be nice if they were highlighted. Unfortunately, the RichTextBox control does not support changing the background color of text in .NET framework version 1.1. Also, extending the control to add this feature is non-trivial. The good news is this feature is supported in the Visual Studio 2005 Beta 1 release.

Conclusion

In this article, I presented an XPath Viewer tool that enables easy testing of XPath queries, and can be used as a tool for either learning about XPath or debugging complex XPath queries. I hope all you XML developers find this tool as useful as I have.

Acknowledgements

I would like to thank Dare Obasanjo for this article idea and Howard Hao for his support in fixing some XPathReader bugs. I would also like to thank Tejal Joshi for reviewing the code in this article and providing useful WinForms tips.

Prajakta Joshi is a Software Design Engineer in Test on the WebData XML team.