C # Use XPath to find a node

Source: Internet
Author: User
The path from the root node can be used to determine the XPath syntax.

/Root/<Node 1>/<Node 2> // <@ attribute>

XPath is very powerful, but it is also a relatively complex technology. It is best to go to the blog site to find some Professional posts for a look, below are some simple XPath syntaxes and an example for your reference.




empire burlesque
Bob Dylan
10.90


hide your heart
Bonnie Taylor
9.90


Greatest Hits
Dolly Parton
9.90


Locate a node
XML is a tree structure, similar to the structure of the data folder in the file system. XPath is similar to the path naming method in the file system. However, XPath is a pattern that selects all nodes in the XML file whose path conforms to a pattern. For example, to select all the price elements in CD under catalog, you can use:

/CATALOG/CD/price

If the beginning of xpath is a slash (/), it indicates that this is an absolute path. If there are two diagonal lines (//) at the beginning, all elements in the file that match the pattern will be selected, even at different layers in the tree. The following syntax Selects all elements in the file called Cd (all layers in the tree will be selected ):

// CD

Select unknown element
Use the asterisk (wildcards, *) to select unknown elements. The following syntax Selects all child elements of/CATALOG/CD:

/CATALOG/CD /*

The following syntax Selects all child elements of catalog, including price as the child element.

/CATALOG/*/price

The following syntax Selects all elements with two parent nodes called Price.

/*/Price

The following syntax Selects all elements in the file.

//*

Note that to access non-hierarchical elements, the XPath syntax must start with two diagonal lines (//). To access unknown elements, use the asterisk (*), an asterisk can only represent an element with an unknown name, but cannot represent an element with an unknown level.

Select Branch
Use brackets to select branch. The following syntax extracts the first element named CD from the child element of catalog. The XPath definition does not contain the 0th element.

/CATALOG/CD [1]

The following syntax selects the last CD element in catalog: (xpathj does not define the first () function. In the preceding example, [1] can be used to retrieve the first element.

/CATALOG/CD [last ()]

The following syntax Selects all/CATALOG/CD elements containing the price sub-element.

/CATALOG/CD [price]

The following syntax Selects all/CATALOG/CD elements whose price value is 10.90.

/CATALOG/CD [price = 10.90]

The following syntax selects the price element of all/CATALOG/CD elements whose value is equal to 10.90.

/CATALOG/CD [price = 10.90]/price

Select more than one path
You can select more than one path by using the or operand (|. For example:

/CATALOG/CD/Title | CATALOG/CD/artist

Select All title and artist Elements

// Title | // artist

Select All title, artist, and price elements

// Title | // artist | // price

Select attributes
In XPath, you can select attributes in addition to elements. All attributes start. For example, select all the attributes in the file called country:

// @ Country

Select all CD elements containing the country attribute:

// CD [@ Country]

The following syntax Selects all CD elements containing attributes

// CD [@ *]

The following syntax selects the CD element whose country attribute value is UK.

// CD [@ Country = 'U']

 

Related Article

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.