using System.Xml.XPath; // Load the XML document XPathDocument doc = new XPathDocument("file.xml"); // Create an XPathNavigator and move to the root element XPathNavigator nav = doc.CreateNavigator(); nav.MoveToRoot(); // Select all the "book" elements and iterate through them XPathNodeIterator iter = nav.Select("//book"); while (iter.MoveNext()) { // Retrieve values from the selected "book" element string title = iter.Current.GetAttribute("title", ""); string author = iter.Current.GetAttribute("author", ""); // Process the values... }
using System.Xml.Linq; using System.Xml.XPath; // Load the XML document XDocument doc = XDocument.Load("file.xml"); // Create an XPathDocument from the document XPathDocument xpathDoc = new XPathDocument(doc.CreateReader()); // Create an XPathNavigator from the XPathDocument XPathNavigator nav = xpathDoc.CreateNavigator(); // Select nodes from the document using an XPath expression XPathNodeIterator iter = nav.Select("//book[price > 10.00]"); // Iterate through the selected nodes while (iter.MoveNext()) { // Retrieve values from the selected node string title = iter.Current.GetAttribute("title", ""); string author = iter.Current.GetAttribute("author", ""); decimal price = Decimal.Parse(iter.Current.GetAttribute("price", "")); // Process the values... }The System.Xml.XPath namespace is part of the .NET Framework Class Library, which is included in the .NET runtime environment.