XPath syntax for C # operations XML

Source: Internet
Author: User
Tags xpath

I've been on it before. NET operation of XML, but not very detailed, now I will explain how C # to operate XML files, just as learning to operate the database to learn the SQL language, before learning to manipulate XML and language, we need to familiarize ourselves with XML "SQL" statement XPath. Since the purpose of this series of posts is not to introduce the XPath syntax in detail, I borrowed a leves post from the garden to briefly introduce XPath syntax:

XPath is the query language of XML and is similar to the role of SQL. Use the following XML as an example to introduce the syntax of XPath.

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
 <cd country="USA">
  <title>Empire Burlesque</title>
  <artist>Bob Dylan</artist>
  <price>10.90</price>
 </cd>
 <cd country="UK">
  <title>Hide your heart</title>
  <artist>Bonnie Tyler</artist>
  <price>9.90</price>
 </cd>
 <cd country="USA">
  <title>Greatest Hits</title>
  <artist>Dolly Parton</artist>
  <price>9.90</price>
 </cd>
</catalog>

Locating nodes

XML is a tree structure, similar to the structure of folders in a file system, and XPath is similar to the path naming of a file system. However, XPath is a pattern that selects all nodes in an XML file where the path conforms to a pattern. For example, to select all the price elements in the CD under catalog, you can use:

/catalog/cd/price  

If the beginning of the XPath is a slash (/) represents this is an absolute path. If the beginning is a two slash (//), all of the elements in the file that conform to the schema are selected, even if they are at different levels in the tree. The following syntax selects all elements in the file called CDs (which are selected at any level in the tree):

//cd

Select an unknown element

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

/catalog/cd/*

The following syntax selects elements in all catalog that contain the price as a child element.

/catalog/*/price

The following syntax selects all elements of a two-layer parent node, called Price.

/*/*/price

The following syntax selects all the elements in the file.

//*

It is important to note that in order to access elements that are not hierarchical, the XPath syntax must start with a two slash (//), the asterisk (*) is used to access the unknown element, and the asterisk can only represent elements of unknown names and cannot represent elements of unknown levels.

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.