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);
- }
Let's start with XPath syntax one by one to select nodes based on your requirement.
Selecting Nodes
Select all Courses and trainings nodes using '//' symbol for all elements.
-
- document.XPathSelectElements("//Courses");
-
-
- document.XPathSelectElements("//Training");
Select root node course by using the '/' symbol.
- var rootcourse = document.XPathSelectElement("TrainingData/Courses");
-
-
- var selectingcurrent = rootcourse.XPathSelectElements("./Training[@name='c# basic']");
-
-
- 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");
Predicates (indexing, last(), @, *)
Select the first course (using the index or last() etc predicates).
- document.XPathSelectElements("//Courses[1]");
-
Using @
-
- var hourAttribite = document.XPathSelectElements("//content[@hours]");
-
-
- var hour2Attribite = document.XPathSelectElements("//content[@hours = 2]");
Using node name direclty
-
- var trainingpricegreat100 = document.XPathSelectElements("//Training[price >= 100]");
Using *
-
- // use '*' to match any type of node where name is equal to data type
- var hourAttribite = document.XPathSelectElements("//*[@name='data types']");
-
- 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!