Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / Windows Forms
Article

yet another XPathTester

Rate me:
Please Sign up or sign in to vote.
2.12/5 (6 votes)
7 Dec 2007CPOL2 min read 23.7K   189   13   2
...trying to be designed ergonomic

Features

  • displays the Xml-File and colorizes all matched Nodes of the tested XPath-Query.
  • Namespace-extraction from Xml-File
  • Save/restore all required Informations of any tested XPath-Query:
    - XmlFile, - contained XmlNamespaces, - XPath-Expression, - XmlNamespaces, which are referenced by the Expression.
  • You can add a comment to each tested XPath-Query.
  • some silly samples

Notice

The XPath-language is designed to specify querys from any arbitrary XmlElement. This XPath-tester only supports queries from the document-node itself.

Screenshot - XPathTester.Png

Using the Program

  1. First select an Xml-File, which you want to examine.
    (A click in the empty row of the selector-grid will open a FileOpenDialog.)
  2. Then write an XPath-Expression.
  3. If the Expression needs to use Namespaces, you can select them from the "select Namespace" - grid.
  4. Caution: some (e.g. the default-) Namespaces have no prefix defined in the XmlFile. To deal with that you need to define yourself the prefix you will use in your XPath-Query (Type it into the "XPathPrefix" - Column).
  5. Now you can click "Markup Query-Result", and I hope, it will.
  6. An invalid query will beep and will markup all in red. Then pay attention to the statusbar, which displays the error-message.
  7. Have a backup of your successful or interesting queries by menu "Database – save".

Use of the Program

The colorizing of specified Nodes nicely shows the result of XPath-queries. That helps you to develop appropriate queries, but can also be useful to understand the function of some complicated Xml-Files (like Dataset.xsd and stuff).

E.g the screenshot shows the query-result of the XPath "//def:Compile/@Include", which queries all files intended to be compiled from a Project-file (*.vbproj).
The complete Sub executing that query may look like this:

VB.NET
Private Sub DisplayCompiledFiles(ByVal Path As String)
   Dim XDoc As New XmlDocument
   XDoc.Load(Path)                '"Path": selected from XmlFile-grid
   Dim NSMngr As New XmlNamespaceManager(XDoc.NameTable)

   '"def": in "known Namespaces" userdefined Namespace-prefix
   '"http://..." : Namespace auto-extracted from the XmlFile
   NSMngr.AddNamespace( _
      "def", "http://schemas.microsoft.com/developer/msbuild/2003")
   With New StringBuilder
      '"//def:Compile/@Include": tested XPath-expression
      For Each XAttr As XmlAttribute In XDoc.SelectNodes( _
            "//def:Compile/@Include", NSMngr)
         .Append(XAttr.Value).Append(ControlChars.Lf)
      Next
      MsgBox(.ToString)
   End With
End Sub

(note: This one can be optimized with use of XPathDocument, XPathNavigator etc.)

Another point of interest is the datatype of the ForEach - iterator.
Its XmlAttribute.
Because querying "//def:Compile/@Include" returns a XmlNodelist containing only XmlAttributes.
Querying "//def:Compile" would return XmlElements. So the loop for that should go like:

VB.NET
For Each Xel As XmlElement In XDoc.SelectNodes( _
      "//def:Compile", NSMngr)
   .Append(Xel.GetAttribute("Include")).Append(ControlChars.Lf)
Next

Good Tutorials

about Xml and stuff

Excuse me

The program can be considered as example of blow-ware, as a result of rapid developement.
The folder "Helpers" contains a bunch of classes I often use in several projects.
So some of these classes are more powerful and complicated as (here) needed, others may look like big solutions of little problems, and again others may look like ? or ??.
Furthermore the Helper-Stuff is commented in german.

A request

Usually I'm rated very badly, and I've no idea about the reason.
IMHO a rate of 1 is appropriate to a solution that doesn't compile or else.
Please send me a mail, or give me a post to make me understand, what code-horror i've done.

thx

History

12/7/07: suggests automatically "def" as XPath-Prefix for Defaultnamespaces


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General[Message Deleted] Pin
konikula6-Jan-10 23:36
konikula6-Jan-10 23:36 
Generalfew final remarks and notes [modified] Pin
konikula7-Jan-10 0:27
konikula7-Jan-10 0:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.