XPath is the content of XML. Here selectnodes is a method of xmldocument or xmlnode in C. Selectnodes uses XPath to select nodes.
Important syntax
Selectnodes ("item ")
Select a node named item from the child node of the current node.
Selectnodes ("/item ")
Select the node named item from the child node of the root node.
Selectnodes ("// item ")
Select a node named item from any node. To highlight this arbitrary position, it is not affected by the current node. That is to say, if the current node is at Layer 100th (exaggerated), you can also select the node named item at the first layer.
Selectnodes (".")
Select the current node.
Selectnodes ("..")
Select the parent node of the current node.
Selectnodes ("// item [@ name]")
Based on selectnodes ("// item"), a restriction is added, that is, the property of name is required.
Selectnodes ("// item [@ name = '000000']")
Based on selectnodes ("// item [@ name]"), a restriction is added, that is, the attribute value of name must be 111. Note that there are quotation marks in the syntax. If there is no quotation marks, it indicates the number type. You can use numbers larger than or less than numbers, for example: selectnodes ("// item [@ v> 333]").
Selectnodes ("// item [1]")
Select the first item. Note that it is the first item, not the second item.
In addition, this refers to the first item between siblings. That is to say, if the parent level has three items, select the first item; if two sons with the same name as item exist in the second item of the parent level, the first son will be selected; if the third item of the parent class has two sons with the same name as the item, the first son will also be selected ......
Selectnodes ("// item [last ()-1]")
The second-to-last Node also refers to the second-to-last node between siblings.
Selectnodes ("// item [position () <= 2]")
The first and second nodes (the position () of the first node is 1.
Selectnodes ("// @ name ")
Selectnodes ("/root/item/@ name") obtains the name attribute of an item.
Select the name attribute. Note that the attribute is selected instead of the node. Use the value property to obtain the property value of the property set.
Selectnodes ("/root/item ")
The item son node under the root node.
Selectnodes ("/root // item ")
Under the root node, whether it is a son, grandson, or grandson ......, All items named item are selected.
Wildcard
- You can use * to indicate any node name, such as selectnodes ("/channel/*/item ");
- Use @ * to indicate any attribute;
- Use node () to represent any type of node;
- Text () indicates a node of the text type, which is actually a node of the text. (The element node is a text node, and the text node is a text node ).
Operator
| Represents or. Selectnodes ("// item | // channel"), and select the item or channel node.
Turn, selectnodes + xpath