XPath tutorial

Basic Path Syntax

The basic XPath syntax is similar to filesystem addressing. If the path starts with the slash / , then it represents an absolute path to the required element.

Example 1

XPath Expression: Select the root element AAA
/AAA
XML:
<?xml version="1.0">
     <AAA>
          <BBB/>
          <CCC/>
          <BBB/>
          <BBB/>
          <DDD>
               <BBB/>
          </DDD>
          <CCC/>
     </AAA>

Example 2

XPath Expression: Select all elements CCC which are children of the root element AAA

/AAA/CCC
XML:
<?xml version="1.0">
     <AAA>
          <BBB/>
          <CCC/>
          <BBB/>
          <BBB/>
          <DDD>
               <BBB/>
          </DDD>
          <CCC/>
     </AAA>

Example 3

XPath Expression: Select all elements BBB which are children of DDD which are children of the root element AAA

/AAA/DDD/BBB
XML:
<?xml version="1.0">
     <AAA>
          <BBB/>
          <CCC/>
          <BBB/>
          <BBB/>
          <DDD>
               <BBB/>
          </DDD>
          <CCC/>
     </AAA>
C# code:
// BasicSyntax - /xpath/basicsyntax.cs
// Copyright 2003 by Johannes Roith

using System;
using System.Xml.XPath;

class BasicSyntax {
        static void Main() {
                
        }

}