. NET XML node read detailed

Source: Internet
Author: User
Tags net xml xpath

. NET XML node read detailed

<?xml version= "1.0" encoding= "Iso-8859-1"?>

<bookstore>

<book>
<title lang= "Eng" >harry potter</title>
<price>29.99</price>
</book>

<book>
<title lang= "Eng" >learning xml</title>
<price>39.95</price>
</book>

</bookstore> Select Nodes
XPath uses a path expression to select nodes in an XML document. Nodes are selected by following the path or step.

The most useful path expressions are listed below:
Description of expression
NodeName Select all child nodes of this node
/Select from root node
Select the nodes in the document from the current node of the matching selection, regardless of their location
. Select the current node
.. Select the parent node of the current node
@ Pick Properties

Instance
In the table below, we have listed some path expressions and the results of expressions:

Path expression Results
Bookstore Select all child nodes of the bookstore element
/bookstore Select the root element bookstore

Note: If the path starts with a forward slash (/), the path always represents an absolute path to an element!

Bookstore/book selects all the book elements that belong to the bookstore child element.
Book selects all book child elements, regardless of their position in the document.
Bookstore//book selects all the book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore.
//@lang Select all properties named Lang.

predicate (predicates)
predicate pragmatics to find a particular node or a node that contains a specified value.

The predicate is embedded in square brackets.

Instance
In the table below, we list some path expressions with predicates, and the results of expressions:

Path expression Results
/BOOKSTORE/BOOK[1] Selects the first book element that belongs to the bookstore child element.
/bookstore/book[last ()] selects the last book element that belongs to the bookstore child element.
/bookstore/book[last ()-1] selects the penultimate book element that belongs to the bookstore child element.
/bookstore/book[position () <3] selects the book element with the first two child elements that belong to the bookstore element.
title[@lang] Select all title elements that have properties named Lang.
title[@lang = ' eng '] selects all the title elements, and these elements have the lang attribute with the value eng.
/bookstore/book[price>35.00] selects the book element of all bookstore elements, and the value of the price element must be greater than 35.00.
/bookstore/book[price>35.00]/title selects the title element of the book element in all bookstore elements, and the value of the price element must be greater than 35.00.

Select Unknown node
XPath wildcard characters can be used to select an unknown XML element.

Wildcard description
* Match any element node
@* matches any attribute node
Node () matches nodes of any type

Instance
In the following table, we list some path expressions and the results of those expressions:

Path expression Results
/bookstore/* Select all child nodes of the bookstore element
* Select all elements in the document
TITLE[@*] Selects all the title elements with attributes.

Select several paths
By using the "|" in a path expression operator, you can select several paths.

Instance
In the following table, we list some path expressions and the results of those expressions:

Path expression Results
Book/title | Book/price selects the title and price elements of all book elements.
Title | Price selects the title and price elements in all documents.
/bookstore/book/title | Price selects all the title elements of the book element that belong to the bookstore element, and all the price elements in the document.

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.