In this blog, I am demonstrating different syntaxes of XPATH on selecting nodes using expression and steps.
I will be using this below XML file as a reference to my demo.
- <?xml version="1.0" encoding="UTF-8"?>
- <TrainingData>
- <Courses name=".net">
- <Training name="c# basic">
- <content name="data types" hours="1"></content>
- <content name="events and delegates" hours="2"></content>
- <price>100</price>
- </Training>
- <Training name=".net core">
- <content name="REST API" hours="2"></content>
- <content name="MVC" hours="2"></content>
- <price>200</price>
- </Training>
- <Training name="VB">
- <content name="data types" hours="1"></content>
- <price>50</price>
- </Training>
- </Courses>
- <Courses name="Javascript">
- <Training name="basics">
- <content name="data types" hours="1"></content>
- <price>100</price>
- </Training>
- </Courses>
- <Courses name="non technical">
- <Training name="negotiation skills">
- <price>60</price>
- </Training>
- <Training name="7 habits">
- <price>70</price>
- </Training>
- </Courses>
- <Others>
- <Training name="Time managment">
- <price>0</price>
- </Training>
- </Others>
- </TrainingData>
Let's read the file first and get the XDocument object to use XPath syntax.
- static void Main(string[] args)
- {
- var location = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath;
- var path = Path.GetDirectoryName(location);
- var filePath = args.Any() ? args[0] : Path.Combine(path, "..you path of xml\\Trainingdata.xml");
- if (!File.Exists(filePath))
- {
- throw new InvalidOperationException();
- }
- var document = XDocument.Load(filePath);
- }
Selecting Nodes
Select all Courses and trainings nodes using '//' symbol for all elements.
Select root node course by using the '/' symbol.
- //select all Courses Node irrespective of where it is defined.
- document.XPathSelectElements("//Courses");
- //select all training node irrespective of where it is defined.
- document.XPathSelectElements("//Training");
- var rootcourse = document.XPathSelectElement("TrainingData/Courses");
- // select the current node by '.' symbol, here root as current node.
- var selectingcurrent = rootcourse.XPathSelectElements("./Training[@name='c# basic']");
- // select all tranings under .net course
- var firstCourse = document.XPathSelectElement("//Courses[@name='.net']");
- var alltrainings = firstCourse.XPathSelectElements("./Training");
Reading multiple elements
Select all others and non technical trainings use '//' for all elements and for union use '|' sysmbol.
- document.XPathSelectElements("//Courses[@name = 'non technical'] | //Others");
Select the first course (using the index or last() etc predicates).
Using @
Using node name direclty
Using *
- document.XPathSelectElements("//Courses[1]");
- // same way can be done for last "//Courses[last()]"
- // select all content node having hour attribute, @ symbol used for attribute.
- var hourAttribite = document.XPathSelectElements("//content[@hours]");
- // select all content node having hour=2 attribute.
- var hour2Attribite = document.XPathSelectElements("//content[@hours = 2]");
- // select all tranings whose price >= 100.
- var trainingpricegreat100 = document.XPathSelectElements("//Training[price >= 100]");
- // select all conent having name as datatype.
- // use '*' to match any type of node where name is equal to data type
- var hourAttribite = document.XPathSelectElements("//*[@name='data types']");
- // OR
- var allconenthaveDatatype = document.XPathSelectElements("//content[@name='data types']");
I hope you liked this blog. The code is attached with a performance test. Let me know any questions you might have in the comments section.
Thanks!

Mike HuynhPosted Mar 26, 2021, 8:40 PM
Is there a way to select all nodes with this criteria?var multipleElements = document.XPathSelectElements("//Courses[@name = 'non technical'] and //Training[price >= 100]"); <Courses name="non technical"> <Training name="negotiation skills"> <price>60</price> </Training> <Training name="7 habits part 2"> <price>100</price> </Training> </Courses> <Courses name="non technical"> <Training name="negotiation skills part 2"> <price>100</price> </Training> <Training name="7 habits"> <price>70</price> </Training> </Courses>
Mike HuynhPosted Mar 26, 2021, 8:37 PM
Great article. Is there a way to select all nodes