Saturday, October 16, 2010

How to find outer html content of a specified html tag in html document

string html = @"
    <html xmlns=""http://www.w3.org/1999/xhtml"" >
    <head>
     <title></title>                   
        <script language=""javascript"">
        </script>
     <style type=""text/css"">
     </style>
    </head>
    <body>
     <form target=""_blank"" id=""searchForm"">
      <a href=""Test.aspx"">Back</a>
            <table>
                <tr>
                    <td>Hello</td>
                </tr>
            </table>
            <p>some paragraph</p>
            <table>
                <tr>
                    <td>Hello</td>
                </tr>
            </table>
     </form>
    </body>
    </html>
";
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    List<string> tables = this.GetTags(html, "table"true);
}
protected List<string> GetTags(string html, string tag, bool outerHtml)
{
    List<string> tables = new List<string>();
    int limit = 0, lb = 0, ub = 0, length = 0;
    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 = outerHtml ? s : lHtml.IndexOf(">", s) + 1;
            limit = ub = outerHtml 
                ? lHtml.IndexOf(endTag, lb) + endTag.Length + 1 
                : lHtml.IndexOf(endTag, lb) - 1;
            tables.Add(html.Substring(lb, ub - lb));
        }
        else limit = s;
    }
    while (limit > 0);
    return tables;
}

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...