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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...