Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / XML
Article

Extracting Node Paths for Matching "InnerText"

Rate me:
Please Sign up or sign in to vote.
1.38/5 (9 votes)
23 Oct 20071 min read 24.2K   108   8   3
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


Written By
Web Developer
United States United States
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!

Comments and Discussions

 
GeneralMy vote of 1 Pin
Patrik Lindström9-Dec-08 3:09
Patrik Lindström9-Dec-08 3:09 
QuestionXPath Pin
evolved23-Oct-07 11:05
evolved23-Oct-07 11:05 
AnswerRe: XPath Pin
PuneWala23-Oct-07 19:13
PuneWala23-Oct-07 19:13 

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.