XPath Expression Syntax : XPath « XML « C# / C Sharp

XPath Expression Syntax


Expression   Description

/            Starts an absolute path that selects from the root node.

             /Order/Items/Item selects all Item elements that are children of an Items element, 
             which is itself a child of the root Order element.

//           Starts a relative path that selects nodes anywhere.

             //Item/Name selects all the Name elements that are children of an Item element, 
             regardless of where they appear in the document.

@            Selects an attribute of a node.

             /Order/@id selects the attribute named id from the root Order element.

*            Selects any element in the path.

             /Order/* selects both Items and Client nodes because both are contained by a root 
             Order element.

|            Combines multiple paths.
             /Order/Items/Item/Name|Order/Client/Name selects the Name nodes used to describe 
             a Client and the Name nodes used to describe an Item.

.            Indicates the current (default) node.

             If the current node is an Order, the expression ./Items refers to the related 
             items for that order.

..           Indicates the parent node.

             //Name/.. selects any element that is parent to a Name, which includes the Client 
             and Item elements.

[ ]          Define selection criteria that can test a contained node or attribute value.

             /Order[@id="1999-01-30.195496"] selects the Order elements with the indicated 
             attribute value.

             /Order/Items/Item[Price > 50] selects products above $50 in price.

             /Order/Items/Item[Price > 50 and Name="Laser Printer"] selects products that match 
             two criteria.

starts-with  This function retrieves elements based on what text a contained element starts with.

             /Order/Items/Item[starts-with(Name, "C")] finds all Item elements that have a 
             Name element that starts with the letter C.

position     This function retrieves elements based on position.

             /Order/Items/Item[position ()=2] selects the second Item element.

count        This function counts elements. You specify the name of the child element to count or 
             an asterisk (*) for all children.

             /Order/Items/Item[count(Price) = 1] retrieves Item elements that have exactly one 
             nested Price element.

Related examples in the same category

3.Select by path
4.Find Elements with an XPath SearchFind Elements with an XPath Search
5.XPath Query Demo
6.Read XML node using the XML path
7.XmlQuery Example
8.Using XPath to get string value, integer value and boolean value
9.Extensions.XPathSelectElement selects an XElement using a XPath expression
10.Extensions.XPathSelectElements Selects a collection of elements using an XPath expression.
11.Returns the string result from evaluating an xpath expression against the given document and context.
12.Gets the text value from the element located by the given XPath.
13.Returns the inner text of the single node selected from the specified xpath if found; otherwise, null.
14.Get an array of nodes matching an XPath expression
15.Schema Reference Util