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.
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <TrainingData>
  3. <Courses name=".net">
  4. <Training name="c# basic">
  5. <content name="data types" hours="1"></content>
  6. <content name="events and delegates" hours="2"></content>
  7. <price>100</price>
  8. </Training>
  9. <Training name=".net core">
  10. <content name="REST API" hours="2"></content>
  11. <content name="MVC" hours="2"></content>
  12. <price>200</price>
  13. </Training>
  14. <Training name="VB">
  15. <content name="data types" hours="1"></content>
  16. <price>50</price>
  17. </Training>
  18. </Courses>
  19. <Courses name="Javascript">
  20. <Training name="basics">
  21. <content name="data types" hours="1"></content>
  22. <price>100</price>
  23. </Training>
  24. </Courses>
  25. <Courses name="non technical">
  26. <Training name="negotiation skills">
  27. <price>60</price>
  28. </Training>
  29. <Training name="7 habits">
  30. <price>70</price>
  31. </Training>
  32. </Courses>
  33. <Others>
  34. <Training name="Time managment">
  35. <price>0</price>
  36. </Training>
  37. </Others>
  38. </TrainingData>
Let's read the file first and get the XDocument object to use XPath syntax.
  1. static void Main(string[] args)
  2. {
  3. var location = new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath;
  4. var path = Path.GetDirectoryName(location);
  5. var filePath = args.Any() ? args[0] : Path.Combine(path, "..you path of xml\\Trainingdata.xml");
  6. if (!File.Exists(filePath))
  7. {
  8. throw new InvalidOperationException();
  9. }
  10. var document = XDocument.Load(filePath);
  11. }
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.
  1. //select all Courses Node irrespective of where it is defined.
  2. document.XPathSelectElements("//Courses");
  3. //select all training node irrespective of where it is defined.
  4. document.XPathSelectElements("//Training");
Select root node course by using the '/' symbol.
  1. var rootcourse = document.XPathSelectElement("TrainingData/Courses");
  2. // select the current node by '.' symbol, here root as current node.
  3. var selectingcurrent = rootcourse.XPathSelectElements("./Training[@name='c# basic']");
  4. // select all tranings under .net course
  5. var firstCourse = document.XPathSelectElement("//Courses[@name='.net']");
  6. var alltrainings = firstCourse.XPathSelectElements("./Training");
Reading multiple elements
Select all others and non technical trainings use '//' for all elements and for union use '|' sysmbol.
  1. document.XPathSelectElements("//Courses[@name = 'non technical'] | //Others");
Predicates (indexing, last(), @, *)
Select the first course (using the index or last() etc predicates).
  1. document.XPathSelectElements("//Courses[1]");
  2. // same way can be done for last "//Courses[last()]"
Using @
  1. // select all content node having hour attribute, @ symbol used for attribute.
  2. var hourAttribite = document.XPathSelectElements("//content[@hours]");
  3. // select all content node having hour=2 attribute.
  4. var hour2Attribite = document.XPathSelectElements("//content[@hours = 2]");
Using node name direclty
  1. // select all tranings whose price >= 100.
  2. var trainingpricegreat100 = document.XPathSelectElements("//Training[price >= 100]");
Using *
  1. // select all conent having name as datatype.
  2. // use '*' to match any type of node where name is equal to data type
  3. var hourAttribite = document.XPathSelectElements("//*[@name='data types']");
  4. // OR
  5. 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!