HtmlAgilityPack using XPath contains method and predicates

HtmlAgilityPack using XPath contains a method

I am using HtmlAgilityPack and I need to know if the class attribute contains a specific word, now I have this page:

<div class="yom-mod yom-art-content "><div class="bd"> <p class="first"> .................... </p> </div> </div> 

I'm doing it:

 HtmlDocument doc2 = ...; List<string> paragraphs = doc2.DocumentNode.SelectNodes("//div[@class = 'yom-mod yom-art-content ']//p").Select(paragraphNode => paragraphNode.InnerHtml).ToList(); 

But this is too specific what I need, it is something like this:

 List<string> paragraphs = doc2.DocumentNode.SelectNodes("//div[contains(@class, 'yom-art-content']//p").Select(paragraphNode => paragraphNode.InnerHtml).ToList(); 

But this does not work, please help me ..

+4
source share
2 answers

Perhaps the problem is that you are missing the closing parenthesis of the contains () function:

 //div[contains(@class, 'yom-art-content']//p v //div[contains(@class, 'yom-art-content')]//p List<string> paragraphs = doc2.DocumentNode.SelectNodes("//div[contains(@class, 'yom-art-content')]//p") .Select(paragraphNode => paragraphNode.InnerHtml).ToList(); 

As a general sentence, please explain what you mean when you say things like "it did not work." I suspect you are getting an error message that might help identify the problem?

+14
source

Instead of using HAP for this, take a look at CsQuery , which provides a jQuery style selector.

It looks especially suitable for what you are trying to do.

CsQuery is the jQuery port for .NET 4. It implements all CSS2 and CSS3 selectors, all DOM manipulation methods for jQuery, and some of the utility methods. Most of the jQuery test suite (since 1.6.2) has been ported to C #.

+1
source

All Articles