views:

288

answers:

1

I am using HtmlAgilityPack. Is there a one line code that I can get all inner text of html, e.g., remove all html tags and scripts?

+1  A: 

Like this:

document.DocumentNode.InnerText

Note that this will return the text content of <script> tags.

To fix that, you can remove all of the <script> tags, like this:

foreach(var script in doc.DocumentNode.Descendants("script").ToArray())
    script.Remove();
foreach(var style in doc.DocumentNode.Descendants("style").ToArray())
    style.Remove();
SLaks
It seems that DocumentNode does not have a function named Descendant?"'HtmlAgilityPack.HtmlNode' does not contain a definition for 'Descendants'"
Yang
What version are you using?
SLaks
HTML Agility Pack V1.3.0.0, is it too old?
Yang
Yes; get a newer version.
SLaks