Mega Code Archive

 
Categories / C# / XML
 

XPathNavigator Evaluate evaluates XPath expression and returns typed result

using System; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Collections; using System.Collections.Generic; using System.Xml.XPath; public class MainClass{    public static void Main(){                    XPathDocument document = new XPathDocument("books.xml");       XPathNavigator navigator = document.CreateNavigator();              Double total = (double)navigator.Evaluate("sum(descendant::book/price)");       Console.WriteLine("Total price for all books: {0}", total.ToString());    } } /* <?xml version="1.0" encoding="utf-8" ?>  <bookstore>     <book genre="Programming" publicationdate="2010-03-22" ISBN="1-111111-11-0">         <title>C#</title>         <author>             <first-name>A</first-name>             <last-name>B</last-name>         </author>         <price>8.99</price>     </book>     <book genre="data" publicationdate="2010-11-17" ISBN="0-201-11111-2">         <title>XML</title>         <author>             <first-name>D</first-name>             <last-name>E</last-name>         </author>         <price>11.99</price>     </book> </bookstore> */