平台,框架&库
现在阅读
Extracting Node Paths for Matching "InnerText"
0

Extracting Node Paths for Matching "InnerText"

由 ultracpy2018年1月26日

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

出处:https://www.codeproject.com/Articles/15796/Extracting-Node-Paths-for-Matching-quot-InnerText

关于作者
ultracpy
评论

你必须 登录 提交评论