Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: HtmlAgilityPack extract inner text

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?

like image 227
Yang Avatar asked May 06 '10 23:05

Yang


2 Answers

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();
like image 52
SLaks Avatar answered Oct 17 '22 11:10

SLaks


I wrote a simple method. It may help you. This method can extract all specific tag's node. Then you can use the HtmlNodeCollection[i].InnerText to get its text.

    HtmlDocument hDoc;
    HtmlNodeCollection nodeCollection;

    public void InitInstance(string htmlCode) {
        hDoc.LoadHtml(htmlCode);
        nodeCollection = new HtmlNodeCollection();
    }
    private void GetAllNodesInnerTextByTagName(HtmlNode node, string tagName) {
        if (null == node.ChildNodes) {
            return ;
        } else {
            HtmlNodeCollection nCollection = node.SelectNodes( tagName );
            if( null != nCollection ) {
                for( int i=0; i<nCollection.Count; i++) {
                    nodeCollection.Add( nCollection[i]);
                    nCollection[i].Remove();
                }
            }
            nCollection=node.ChildNodes;
            if(null != nCollection) {
                for(int i=0;i<nCollection.Count; i++) {
                    GetAllNodesInnerTextByTagName( nCollection[i] , tagName );
                }
            }
        }
like image 37
tsingroo Avatar answered Oct 17 '22 10:10

tsingroo