tags:

views:

25

answers:

1

I have a Xml file, ( actually HTML tags ) , I want to use the SelectNodes method to get ANY node/tag containing a keyword. The node name can be anything <td> , <div> <p>. What is the XPath to get any node which contains a keyword?

+3  A: 

This XPath expression:

/html/body//*[text()[contains(.,'keyword')]]

This means: any element descendant of body having a text node child with the string 'keyword' contained in its string value.

Edit: Better predicate 'cause probably there is more than one text node child...

Alejandro
+1 for a precise answer.
Dimitre Novatchev
Thanks for your reply. This works for some pages and some other pages throw exception. Of course all of them contain Body and HTML Tags. Do you have any idea what can cause to through exception?? ( Object reference not set to an instance of an object )
If you have code that throws an exception then post the minimal code and a minimal input that allows us to reproduce and understand the exception. I don't see why `For Each el As XmlElement In someXmlDocument.SelectNodes("/html/body//*[contains(text(), 'keyword')]` would throw an exception.
Martin Honnen
@user445891: Maybe you are trying to do something with an empty node set?
Alejandro