RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:9:30-18:00
你可能遇到了下面的问题
关闭右侧工具栏
Building an XPath Visualizer with Windows Forms
  • 作者:xiaoxiao
  • 发表时间:2020-12-23 11:03
  • 来源:未知

Click here to download the code sample for this article.

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:

A simple, lightweight application that can be used to test XPath expressions. Gives a simple "notepad" like view of XML as it is stored in the file, rather that a tree view (Internet Explorer style). Highlights selected XPath nodes in XML. Shows properties of each matched node in a grid view. Allows users to perform queries with namespaces. Allows users to easily add/remove namespace prefix and URI values in a provided namespace manager. 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.

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