Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HtmlAgilityPack, using XPath contains method and predicates

HtmlAgilityPack, using XPath contains method

I'm using HtmlAgilityPack and i need to know if a 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 this:

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

But it's too much specific that I need is something like this:

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

But it don't work, please help me..

like image 749
Oscar Acevedo Avatar asked Feb 04 '13 19:02

Oscar Acevedo


1 Answers

Perhaps the issue is simply that you're missing the closing parenthesis on 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 suggestion, please explain what you mean when you say things like "it didn't work". I suspect you're getting an error message that might help track down the issue?

like image 101
JLRishe Avatar answered Oct 12 '22 11:10

JLRishe