c# - Xml Parsing using SelectNodes and Xpath -


i'm writing program parse xml file (with book contents).

what 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);     } // highest priced book(s) prices.sort(); 

what wanna use selectnodes highest priced books, , return xmlnodelist

//to store highest price of book string highest = prices[0];  **// can't figure out xmlnodelist expensivelist = xmldoc.selectnodes("descendant::book[price = highest]");** 

any appreciated, thanks!

edit: managed around making foreach loop nodes in booklist if case compare price.innertext highest. it's working i'd still know if can done xpath. thanks!

edit #2: can improved upon using different approaches, wanna know if possible compare string variable node values xpath.

if decided use linq2xml code similar to: (since don't have xml, came with)

// load file var xdoc = xdocument.load(path);  // find nodes tagname book var books = xdoc.descendants("book")  var expensivelist = books    // make sure book has price node   .where(b => b.descendants("price").firstordefault() != null      // compare first price node value highest     && b.descendants("price").first().value.equals("highest",       stringcomparison.ordinalignorecase))    // lets return 10   .take(10)    // return found list<xelement>   .tolist(); 

if price integer then:

   int tempparse;     ...         // make sure book has 1 price   .where(b => b.descendants("price").count() == 1      // price integer (you can replace decimal)     && int.tryparse(b.descendants("price").first().value, out tempparse))    // make temporary anonymous object sort price   .select(b => new    {     book = b,     price = int.parse(b.descendants("price").first().value)   })    // order anonymous object price   .orderbydescending(b => b.price)    // select books   .select(b => b.book)    // lets return 10   .take(10)     // return found list<xelement>   .tolist(); 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -