SelectNodes use of XML and XPath

Source: Internet
Author: User

XPath is the content of XML, where SelectNodes is a method of XmlDocument or XmlNode in C #. SelectNodes uses XPath to select nodes.

Important syntax

SelectNodes ("item")

Select the node named item from the son node of the current node.

SelectNodes ("/item")

Select the node named item from the son node of the root node.

SelectNodes ("//item")

Select the node named item on the node from any location. To focus on this arbitrary position, it is not affected by the current node, that is, if the current node is on the 100th level (a bit exaggerated), you can also select the first layer of the node named item.

SelectNodes (".")

Select the current node.

SelectNodes ("..")

Selects the parent node of the current node.

SelectNodes ("//item[@name]")

On the basis of selectnodes ("//item"), a limitation is added, which is the requirement to have the name attribute.

SelectNodes ("//item[@name = ' 111 ']")

On the basis of selectnodes ("//item[@name]"), a limitation was added, which is to require the Name property value to be 111. Note that there is a quotation mark in the syntax, and if there is no quotation mark, it is a number type, and for numeric types you can use greater than, less than, and so on, such as: selectnodes ("//item[@v>333]").

SelectNodes ("//item[1]")

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

And here is the first item between brothers, that is to say: If the parent has three item, then select the first; if the parent of the second item has two names of the same item's son, then the first son will be selected; If the parent third item has two names, the same item Son, the first son will also be chosen ...

SelectNodes ("//item[last ()-1]")

The second-to-last node, also refers to the second-to-last brother between the two.

SelectNodes ("//item[position () <=2]")

The nodes with the first and second positions (the position () of the first node are 1), which is also the location of the brothers.

SelectNodes ("//@name")

SelectNodes ("/root/item/@name") takes the Name property of item

Select the Name property, and Note that the attribute is selected instead of the node. Gets the property value of the property collection with the Value property.

SelectNodes ("/root/item")

Root node of the item son node under root.

SelectNodes ("/root//item")

Root node Whether it is a son, grandson, heavy grandson ..., as long as the name of the item is selected.

Wildcard characters

    • Available * denotes any node name, such as selectnodes ("/channel/*/item");
    • Use @* to express any attribute;
    • Use node () to represent any type of nodes;
    • Use text () to represent the text type of the node, which is actually the representation of the text node (the element node below is the text node, text node below is the text).

operator | Represents or. SelectNodes ("//item|//channel"), select the item or channel node.

Transferred from: http://blog.csdn.net/eric_guodongliang/article/details/7187880

SelectNodes use of XML and XPath

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.