Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

yet another XPathTester

0.00/5 (No votes)
7 Dec 2007 1  
...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:

   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:

         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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here