C # XPath syntax for XML operations

Source: Internet
Author: User
I have also sent a message about. net, but it is not very detailed. Now I will introduce in detail how to operate XML files in C #, just like learning to operate a database and learn the SQL language, before learning to operate XML and the language, we need to familiarize ourselves with the XML "SQL" Statement XPath. Since this series of posts does not aim to introduce the XPath syntax in detail, I borrowed leves from the garden post to briefly introduce the XPath Syntax:

XpathYesXMLQuery Language, andSQL. BelowXMLFor exampleXpath.

 
 
 
<? 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 Taylor </artist>
 
<Price> 9.90 </price>
 
</Cd>
 
<CD Country = "USA">
<Title> Greatest Hits </title>
 
<Artist> Dolly Parton </artist>
 
<Price> 9.90 </price>
 
</Cd>
 
</CATALOG>
 
 
 
 

Locate a node
XMLIs a tree structure, similar to the structure of data folders in the file system,XpathIt is also similar to the path naming method of the file system. HoweverXpathIs a Mode(Pattern), Can be selectedXMLIn the file, all nodes whose paths match a certain pattern come out. For example, selectCatalogBottomCDAllPriceElements can be used:

 
 
 
/CATALOG/CD/price
 
 

IfXpathIs a diagonal line (/) Indicates that this is an absolute path. If it starts with two diagonal lines (//) Indicates that all elements in the file that match the pattern will be selected, even at different layers in the tree. The following syntax Selects allCD(All layers in the tree will be selected ):

 
 
 
// CD
 
 

Select unknown element
Use asterisks (Wildcards,*) You can select unknown elements. The following syntax is selected:/CATALOG/CDAll child elements:

 
 
 
/CATALOG/CD /*
 
 

The following syntax Selects allCatalogContainsPriceAs a child element.

 
 
 
/CATALOG/*/price
 
 

The following syntax selects two parent nodes:Price.

 
 
 
/*/Price
 
 

The following syntax Selects all elements in the file.

 
 
 
//*
 
 

Note that you want to access non-hierarchical elements,XpathThe syntax must start with two diagonal lines.(//)To access unknown elements, use the asterisk(*), Asterisks can only represent elements with unknown names, but cannot represent elements with unknown levels.

Select Branch
Use brackets to select branch. The following syntaxCatalogThe first sub-element namedCD.XpathNo0Element.

 
 
 
/CATALOG/CD [1]
 
 

Select the following syntaxCatalogThe lastCDElements :(XpathjNot DefinedFirst ()This function is used in the previous example.[1]You can retrieve the first element.

 
 
 
/CATALOG/CD [last ()]
 
 

The following syntax selectsPriceAll sub-elements/CATALOG/CDElement.

 
 
 
/CATALOG/CD [price]
 
 

Select the following syntaxPriceThe element value is equal10.90All/CATALOG/CDElement

 
 
 
/CATALOG/CD [price = 10.90]
 
 

Select the following syntaxPriceThe element value is equal10.90All/CATALOG/CDElement OfPriceElement

 
 
 
/CATALOG/CD [price = 10.90]/price
 
 

Select more than one path
UseOrOperands(|)You can select more than one path. For example:

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

Select AllTitleAndArtistElement

 
 
 
// Title | // artist
 
 

Select AllTitleAndArtistAndPriceElement

 
 
// Title | // artist | // price
 
 

Select attributes
InXpathIn addition to selecting elements, you can also select attributes. All attributes are@. For example, select allCountryProperties:

 
 
 
// @ Country
 
 

Select AllCountryThis attributeCDElement:

 
 
 
// CD [@ Country]
 

The following syntax Selects allCDElement

 
 
 
// CD [@ *]
 
 

Select the following syntaxCountryThe property value isUKOfCDElement

 
 
 
// CD [@ Country = 'U']

Once you have mastered the XPath syntax, You can theoretically access any node and any value in the XML file.

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.