Parse a div with HTML Agility Pack

c# html html-agility-pack html-parsing parsing

Question

I've this HTML code:

div class="singolo-contenuto link_azure">
<p><img src="" class="left pad2 field_foto" alt="" /><p> Message </p>    
</div>

I need to "capture" "Message". I'm trying with:

String message = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='singolo-contenuto link_azure']").InnerText;

but doesn't works... I obtain a lot of the full page... what's wrong?

Popular Answer

The XPath expression you have just gets you to the <div> tag. You need to get deeper into the last <p> tag. This will work:

var message = htmlDoc.DocumentNode.SelectSingleNode("//div[@class='singolo-contenuto link_azure']//p[last()]").InnerText;



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why