Selectnodes and xpath

Source: Internet
Author: User

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 ")

SlaveCurrent NodeOfSon NodeSelect the node named item.

Selectnodes ("/item ")

SlaveRoot NodeOfSon NodeSelect the node named item.

Selectnodes ("// item ")

SlaveAny locationSelect a node named item. To highlight this arbitrary location, itNot affected by the current nodeThat is to say, if the current node is in the 100th layer (a little 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 the syntax contains quotation marks;If no quotation marks are provided, it indicates a number,You can use numbers greater than or less than numbers, for example, selectnodes ("// item [@ v> 333]").

Selectnodes ("// item [1]")

Select the first item,Note that it is the first, not the second.

And here it refersBrothersThe first item, that is, if the parent level has three items, select the first item. If the second item of the parent level has two sons with the same name as the item, 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 attributes are selected,Instead of nodes. 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 nodeWhether it's son, grandson, or grandson ......, As long as all items 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.