简体   繁体   中英

Search XML node value without knowing parent node

I want to search for an XML nodes value without knowing the node's parent. I read in XML docs that to search for a value, you can use the following syntax:

//book[price>35.00]

This selects all the book elements anywhere in the xmldocument that have a price element with a value greater than 35.00

What I want to do is not reference the book node but instead find all price elements anywhere in the xmldocument with a value greater than 35.00. How do I write this?

I am using XmlDocument classes in C#.

Use this XPath: //price[. > 35.00] //price[. > 35.00]

Kirill has shown an XPath approach - I would personally try to use LINQ to XML (so XDocument instead of XmlDocument ) if you're using .NET 3.5 or higher.

You'd then use:

var prices = doc.Descendants("price")
                .Where(x => (decimal?) x > 35m);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM