Click here to Skip to main content
Licence 
First Posted 3 Oct 2006
Views 15,410
Bookmarked 8 times

Extracting Node Paths for Matching "InnerText"

By | 23 Oct 2007 | Article
This is useful when you need to find node paths for exact matching innertext of nodes.

Introduction

With this XPathFinder you will be able to find XPaths for InnerText you want to find.
This is very simple logic and do not require any expertise in .NET.

What you want to know?

1) .NET
2) XML
3) XPaths

What is this exactly?

Take following example :

<DATA><PARENT>
<CHILD>Value</CHILD>
<CHILD256>Value2</CHILD256>
<XCHILD>Value</XCHILD> <PARENT2>Value
<CHILD2>V</CHILD2>
<CHILD2>ValuE</CHILD2>
<CHILD21>Value</CHILD21>
</PARENT2></PARENT>
</DATA>

Here you want to find all Element Nodes which are having InnerText as "Value".
Possible nodes are :

1) DATA/PARENT/CHILD
2) DATA/PARENT/XCHILD
3) DATA/PARENT/PARENT2
4) DATA/PARENT/PARENT2/CHILD21

This utility can be used to find these XPaths.

Advancements:

You may want to extract node paths along with indexes.
For example :

<DATA><PARENT>
<CHILD>Value</CHILD>
<CHILD>Value</CHILD></PARENT>
</DATA>

Here you want to find all Element Nodes which are having InnerText as "Value".
Possible nodes as :

1) DATA[1]/PARENT[1]/CHILD[1]
2) DATA[1]/PARENT[1]/CHILD[2]

What is used?

To traverse in XML document and build the XPaths, XPathNavigator is used.
I have used Stack to maintain the node names. Each and every node name is inserted in stack while finding the value. If matching value is found, corresponding node names are poped out and Xpath is built and then inserted in Queue. One recusrive function is used to go inside the Element nodes.

Improvements Needed

As recursive function and stack is used, this logic may take more time for Large xml document. I am working on it to find the performance efficient logic.

Code Download Here

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

About the Author

PuneWala

Web Developer

India India

Member

He is just an ordinary developer and curious about new technologies. Fond of detailed analysis of How Stuff Works! He likes debugging, trouble-shooting and making out the application of difficulties. Finding another ways to achieve the result is his passion!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberPatrik Lindström3:09 9 Dec '08  
QuestionXPath Pinmemberevolved11:05 23 Oct '07  
AnswerRe: XPath PinmemberHemant_Joshi19:13 23 Oct '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 23 Oct 2007
Article Copyright 2006 by PuneWala
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid