Home >Backend Development >XML/RSS Tutorial >xml learning (6) in c#Xpath example
Reprint: http://www.php.cn/
In c#, we often search and traverse nodes, this is what we can use XPath syntax, Example Xml:
<?xml version="1.0" encoding="utf-8" ?> <pets> <cat color="black" weight="10"> <price>100</price> <desc>this is a black cat</desc> </cat> <cat color="white" weight="9"> <price>80</price> <desc>this is a white cat</desc> </cat> <cat color="yellow" weight="15"> <price>80</price> <desc>this is a yellow cat</desc> </cat> <dog color="black" weight="10"> <price>100</price> <desc>this is a black dog</desc> </dog> <dog color="white" weight="9"> <price>80</price> <desc>this is a white dog</desc> </dog> <dog color="yellow" weight="15"> <price>80</price> <desc>this is a yellow dog</desc> </dog> </pets>
XPath syntax:
1. Symbols in XPath
Example |
Example description |
means selecting from the root node |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
/pets Select the root node pets |
Represents the spacer between the node and the child node | /pets/dog | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
//xx |
means searching from the entire xml document, regardless of the current node position | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
//price Select all price nodes in the document |
. |
Single English A half-width period indicates selecting the current node | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
/pets/. Selecting the pets node |
.. |
##Double dot indicates selecting the parent node |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
represents the pets node, which is the parent node of the first dog node |
# #@xx | represents the selection attribute | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Indicates selecting the color attribute set of all dog nodes |
[…] | 中The brackets indicate the selection conditions, and the conditions inside the brackets are | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
All dogs with the color white Node |
//dog[/price1fc6ee21d99fd6e669e2125f4bbad298 Greater than >= Greater than or equal to < Less than <= Less than or equal to and and with the relationship or or or the relationship
4. XPath Axes Literally translated this is XPath The meaning of axis, but according to my understanding, it is more appropriate to translate it into XPath node relationship operation keyword, which is a set of keywords plus :: double colon to indicate a node or a group of nodes that are related to the current node. Use syntax: axisname::nodetest[predicate] That is, axis name::node name [get node condition] The specific description is as follows:
|