Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

Extracting Node Paths for Matching InnerText

1.38/5 (9 votes)
23 Oct 20071 min read 1   108  
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