Building an XPath Visualizer with Windows Forms

xiaoxiao2021-03-06  99

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

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 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 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 RowChanged Events Arede 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 iperate through matching nodes.

While (this.xpr.readuntilmatch ())

{

Switch (this.xpr.nodetype)

{

Case XMLNodetype.efficient:

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.SIGNIGNIGNIFICANTWHITESPACE:

Case XMLNodetype.Text:

StartPOS = this.getStartPosition (xtr.LineNumber, xtr.LinePosition);

RichtextBoxxml.select (startpos, xpr.value.length);

THIS.AddNode ();

Break;

Case XMLNodType.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 Defe"

Xpr.NodeType, "Bug in XPathreader", MessageBoxButtons.ok; Break;

}

}

As you can see, some XmlNodeType values ​​can not 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 .

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.

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

Conclusion

In this article, I presented 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 an .

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.

转载请注明原文地址:https://www.9cbs.com/read-124756.html

New Post(0)