I'm writing a program to parse XML file (with Book contents).
What I did,
XmlDoc = new XmlDocument();
XmlDoc.Load(path);
bookList = XmlDoc.GetElementsByTagName("book");
List<string> prices= new List<string>();
foreach (XmlNode node in bookList)
{
XmlNode price = node["price"];
prices.Add(price.InnerText);
}
// to get the highest priced book(s)
prices.Sort();
What I wanna do now is use SelectNodes to look for the highest priced books, and return it as a XMLNodeList
//to store the highest price of a book
string highest = prices[0];
**// this is what i can't figure out
XmlNodeList expensiveList = XmlDoc.SelectNodes("descendant::book[price = highest]");**
Any help is appreciated, thanks!
EDIT: I managed to get around it by making a foreach loop for nodes in bookList with a if case to compare price.InnerText with highest. It's working perfectly but I'd still like to know if this can be done with XPath. Thanks!
EDIT #2: I get that it can be improved upon using different approaches, I just wanna know if it is possible to compare string variable with node values with XPath.