Building an XPath Visualizer with Windows Forms

原创 2004年08月21日 21:12:00

Click here to download the code sample for this article.


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 )

private void namespaces_RowChanged( object sender, DataRowChangeEventArgs e )

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,
   XmlParserContext parserContext = new XmlParserContext(new NameTable(),
    null, null, XmlSpace.Default);
   this.xtr = new XmlTextReader(strmTemp, XmlNodeType.Element, parserContext);   
   this.xtr = new XmlTextReader(xmlFile);   
this.xpr = new XPathReader(xtr, xpc);

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);

      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);
      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:
      MessageBox.Show("XPath 1.0 data model does not define node of type "
 + xpr.NodeType, "Bug in XPathReader", MessageBoxButtons.OK);

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.


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.


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.


jmeter之断言 18.5.5 BeanShell Assertion The BeanShell Assertion allows the user t...
  • wanglha
  • wanglha
  • 2014年12月05日 23:24
  • 2350

在实战中成长Windows Forms开发之路(奋斗的小鸟)_PDF 电子书

  • tjoy2005
  • tjoy2005
  • 2012年12月21日 20:34
  • 1205

Windows Forms 2.0 Programming (2nd Edition)

Windows Forms 2.0 Programming (2nd Edition) (Microsoft .NET Development Series) by Chris Sells, Mich...
  • linhanshi
  • linhanshi
  • 2007年02月06日 00:01
  • 1138

[winform入门级好书]c# Windows Forms程序设计

书名:c# Windows Forms程序设计作者:Erik Brown[美]译者:朱毓斌、吴飞出版社:清华大学出版社售价:78元个人书评:    本书操作步骤详细,完全可以与step bystep级...
  • daily0403
  • daily0403
  • 2006年04月21日 10:06
  • 514

用户界面控件集合Essential Tools for Windows Forms详细介绍使用方法及下载地址

Essential Tools for Windows Forms是一款用户界面控件集合,包含了近80多个界面元素,其中有Ribbon、停靠面板和窗体、编辑器、菜单、MDI等,开发人员使用该控件可以很...
  • fanganzhuanjia
  • fanganzhuanjia
  • 2014年05月10日 11:14
  • 925

Essential Studio for Windows Forms发布2017 v2,支持office 2016和主题定制

Essential Studio for Windows Forms是一个帮您创建高性能的Windows应用程序的Windows Forms界面控件,其包含了高性能的Windows应用程序开发中所需的...
  • Harriet666
  • Harriet666
  • 2017年05月22日 17:10
  • 344

如何在VC++中使用Windows Forms (VS2012-VS2015)
  • softfox
  • softfox
  • 2015年08月23日 12:48
  • 3010


看别人显示播放wav文件显示频谱写的代码都是断断续续的,在这里我贴了完整的代码,给有需要的人做参考,显示频谱还没有完成,不知道怎么弄,已经可以得到byte[] fft数据了,参考别人的写法也可以开方取...
  • ct872587129
  • ct872587129
  • 2015年04月01日 10:32
  • 3015

2.C#编程学习—— Windows Forms

2.C#编程学习—— Windows Forms Windows Form ,是 Microsoft Windows 应用程式开发的新平台,以 .NET Framework 为基础。这种架构提供清晰...
  • notbaron
  • notbaron
  • 2016年11月21日 23:05
  • 30220

初识Windows Forms窗体

Visual Studio是一套完整的开发工具集 RAD工具(Rapid Applivcation Developement) 创建用户界面时,把控件从工具箱拖放到窗体上,把他们放在应用程...
  • Czhenya
  • Czhenya
  • 2017年07月30日 14:27
  • 244
您举报文章:Building an XPath Visualizer with Windows Forms