using System.Xml.Linq; using System.Xml.XPath; XDocument doc = XDocument.Load("example.xml"); XPathNavigator navigator = doc.CreateNavigator(); XPathNodeIterator iterator = navigator.Select("//product"); while (iterator.MoveNext()) { Console.WriteLine(iterator.Current.Value); }
XDocument doc = new XDocument( new XElement("root", new XElement("product", new XElement("name", "Product 1"), new XElement("price", "10.0") ), new XElement("product", new XElement("name", "Product 2"), new XElement("price", "20.0") ) ) ); XPathNavigator navigator = doc.CreateNavigator(); XPathNodeIterator iterator = navigator.Select("//product"); while (iterator.MoveNext()) { Console.WriteLine(iterator.Current.Value); }In the above example, we create an XDocument object with two `product` elements and their child elements. We then create a navigator object and use it to query for all `product` elements in the XML document. Package/Library: System.Xml.Linq