Click here to Skip to main content
15,900,378 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 

  • for it's not quite usual (or usual for you) to recieve "carpet bombing" revision (or just c. commenting) on articles, you may rather read my comments in their original order (if you have such option through email notification*; I hope so), because I can't ensure you, that they are "standalone".. maybe they are.

    *don't follow text but link from notifications, because I densely edited my posts
  • it was pretty good journey - all your articles, in the positive meaning of journey. however it's not possible to absorb all the knowledge, so please don't take me as some genius, just for I read all of your articles in line, nor consider your articles poor, or easy, none of these supposition would be right. it's very clear that I don't remember everything, and that I learned less than if I read article per day, but that is not such important. I learned a lot, I will remember few, and I know that I can always return. BTW only generally problematic article was that bezier one. Take all of my positive ratings (verbatim all those sentences) serious, because I really paid attention in measure necessary for me on each article to get its goal, idea and implementation.
  • under this article you're curious for your low rating. I am sure that I gave you some idea on this topic in one of my comments (I don't know where, and which idea, but I know that I did). I am really glad to meet programmer like you (actually to have the luck to read his articles), but actually it doesn't relates to your talent, IQ or anything like that, I am glad for some more internal reasons, as like "found someone better with similar strategy"... or like that. also sorry for tone of this unordered list, it actually sounds like I had speech on your funeral, or as you read my testament. it's somehow necessary, as I'm kind of remarking on your whole 'till-now' work on here. and you know, that having someone in bookmarks means many times keeping memory of him, or sometime neither this. but who knows. Really keep doing what you do, it's pretty intelligent and hopefully useful for someone (there's high probability that most useful thing ever discovered BY MAN will be never ever used BY ANY MAN at all, this sentence can be prooved by Einsteins only true hypothesis "there's nothing more stupid than people")
  • If you after all these points want to consider me genius, I appreciate it. Your fault Wink | ;)
  • If all my too tragic comments caused you some psychical (or physical) issues, please let me know, but generally I am looking forward to see your next work, or also to start some discussion, under your or mine articles, if you wish. But seriously, just tell me, and I will not comment on your articles anymore (because unusual thing can turn to unwelcome thing, at emotional expense of reciever and moral expense of sender /of those unusual things/). I just descibe this option, because 'carpet commenting' somehow reminds of 'stalking', so there is probability of similar consequences to involved sites.
  • another good point for you is, that although I am bit overflowed by information (so my mind narrows possible input, and I feel dumb a lot), I have no headache, as it is usual after reading too much from stupid, noninformative or language-wrong text. Rose | [Rose] So at least your articles are interesting, easy to read and well formed, designed and without typographic issuesRose | [Rose] . So if you want Math.Floor my 'too high rating' take 'rosed' consideration as a FACT.
  • If you select to communicate with me here or there, let me know if I made you headache Wink | ;)
  • PS: if you are in doubts why I did this expensive step with you (like reading and commenting all), notice my first comment. Simply the article which I practically didn't read (with exception), concretely article with flexible layout and supposingly hand made control splitters (I found this your practice in following article, so I suppose you to use this control in that form with 'pretty woman' / or 'willi woman'), ...e e ... simply that article with image of woman was so good, that I had to check whatever you wrote. It's like Kant's 'knowledge(fact) a priori'. Even if 'knowledge a priori' is stupid term with no sense. I mean - that article is too big rock of gold (for me) to not to fire whole hill with dynamite. WTF | :WTF:


Best regards, Matt

P.S:
I moved my "general response to your request" to my blog http://sah.blog.cz/1001/general-response-to-your-rating-request[^], for I don't consider it to be to-topic thing. And to it it is pretty long, and it can be commenter-repelent to recieve three pages from single member. If you wish to change calling, anonymize or something like that on that blog page, just mail me at k0n1kula[at]p05+.cz. (you must decode numbers and plus back to letters. 1 stays for 'i')

modified on Thursday, January 7, 2010 9:38 AM

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.