Tuesday, August 03, 2010

How to find inner html of spefied html tag in html document

This script grabs all the html content inside specified tag name. For an example following scrip create a lost of content grabbing all the content between script tags. If you are looking to find tag by itself and access attributes etc, please refer this article.
protected override void Render(HtmlTextWriter writer)
{            
    string tag = "script";
    List<string> scripts = new List<string>();
    int limit = 0, lb = 0, ub = 0, length = 0;
    StringBuilder sb = new StringBuilder();
    HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(sb));
    base.Render(htw);
    string html = sb.ToString();
    length = html.Length;
    string startTag = string.Format("<{0}", tag);
    string endTag = string.Format("</{0}", tag);
    string lHtml =  html.ToLower();
    do
    {
        int s=html.ToLower().IndexOf(startTag, limit);
        if (s > 0)
        {
            lb = lHtml.IndexOf(">", s) + 1;
            limit = ub = lHtml.IndexOf(endTag, lb) - 1;
            scripts.Add(html.Substring(lb, ub - lb));
        }
        else limit = s;
    }
    while (limit > 0);
    writer.Write(html);
}

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...