XDocument reads all xml elements and XPath syntax

Source: Internet
Author: User

        Everyday Italian    Giada De Laurentiis    2005    30.00        Harry Potter    J K. Rowling    2005    29.99        XQuery Kick Start    James McGovern    Per Bothner    Kurt Cagle    James Linn    Vaidyanathan Nagarajan    2003    49.99          Learning XML    Erik T. Ray    2003    39.95    

The above is the BookStore. xml file

Start processing

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);            IEnumerable<XElement> elements = xroot.Elements();             (XElement item           Console.WriteLine(item.Name);                DiGuiNode(item);             }            
           (xroot!= ( item                     Console.WriteLine(item.Value);

Running result:

2. XPath Syntax:

/
.
Select attributes.

2.1

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);            IEnumerable<XElement> elements = xroot.XPathSelectElements();             (XElement item 

Running result:

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

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            IEnumerable<XElement> elements = xroot.XPathSelectElements();             (XElement item 

Running result:

2.3 // select the nodes in the document from the current node that matches the selected node, regardless of their location.

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);            IEnumerable<XElement> elements = xroot.XPathSelectElements( (XElement item 

Running result:

2.4. Select the current node.

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);<XElement> elements = xroot.XPathSelectElements();             (XElement item 

Running result:

2.5 .. indicates selecting the parent node of the current node.

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);            IEnumerable<XElement> elements = xroot.XPathSelectElements();             (XElement item = item.XPathSelectElement();

Running result:

2.6 @ indicates selecting elements with this attribute

            XDocument xdoc = XDocument.Load();            XElement xroot = xdoc.Root;            Console.WriteLine(xroot.Name);            IEnumerable<XElement> elements = xroot.XPathSelectElements();             (XElement item 

Running result:

Predicates)

It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.

Instance

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

Path expression Result
/Bookstore/book [1] Select the first book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()] Select the last book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()-1] Select the penultimate book element that belongs to the bookstore sub-element.
/Bookstore/book [position () <3] Select the first two bookstore sub-elements.
// Title [@ lang] Select all the title elements with the lang attribute.
// Title [@ lang = 'eng'] Select All title elements and these elements have the lang attribute whose value is eng.
/Bookstore/book [price> 35.00] Select all the book elements of the bookstore element, and the value of the price element must be greater than 35.00.
/Bookstore/book [price> 35.00]/title Select all the title elements of the book element in the bookstore element, and the value of the price element must be greater than 35.00.
Select unknown Node

The XPath wildcard can be used to select unknown XML elements.

Wildcard Description
* Match any element node.
@* Match any attribute node.
Node () Match any type of nodes.
Instance

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

Path expression Result
/Bookstore /* Select all child elements of the bookstore element.
//* Select all elements in the document.
// Title [@ *] Select All title elements with attributes.
Select several paths

You can select several paths by using the "|" operator in the path expression.

Instance

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

Path expression Result
// Book/title | // book/price Select all the title and price elements of the book element.
// Title | // price Select all the title and price elements in the document.
/Bookstore/book/title | // price Select all the title elements of the book element that belongs to the bookstore element and all the price elements in the document.

The above table information from http://www.w3school.com.cn/xpath/xpath_syntax.asp
      

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.