Results 1 to 5 of 5

Thread: [RESOLVED] [2.0] Convert HtmlTable to HTML source code

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Resolved [RESOLVED] [2.0] Convert HtmlTable to HTML source code

    Hi,

    consider the following code snippet:

    Code:
                HtmlTable table = new HtmlTable();
                HtmlTableRow row1 = new HtmlTableRow();
                
                HtmlTableCell cell1 = new HtmlTableCell();
                HtmlTableCell cell2 = new HtmlTableCell();       
    
                cell1.InnerText = "Test1";
                cell2.InnerText = "Test2";
    
                row1.Cells.Add(cell1);
                row1.Cells.Add(cell2);
    
                table.Rows.Add(row1);
    Now my problem is that I want to obtain the HTML source code of the "table" such that I could output this in a browser control of a windows application. Anyone ever done this or can help me on this? I've tried using HtmlTextWriter with no success. I'm probably not doing the correct thing here...
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2.0] Convert HtmlTable to HTML source code

    Can you just write the HTML and set that to your WebBrowser's DocumentText property instead of doing all the other stuff?
    Code:
            private void button1_Click(object sender, EventArgs e)
            {
                string html = @"
    <table border=""1"">
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td>row 2, cell 1</td>
            <td>row 2, cell 2</td>
        </tr>
    </table>";
    
                this.webBrowser1.DocumentText = html;
            }

  3. #3

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: [2.0] Convert HtmlTable to HTML source code

    The document I want to generate is far more complex than the sample I posted, so instead of having to recreate an HTML parser i'm looking at alternative solutions...
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  4. #4
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: [2.0] Convert HtmlTable to HTML source code

    Would this do it?
    Code:
    using (MemoryStream dataStream = new MemoryStream())
    {
    	using (StreamWriter textWriter = new StreamWriter(dataStream, Encoding.UTF8))
    	{
    		using (HtmlTextWriter htmlWriter = new HtmlTextWriter(textWriter))
    		{
    			table.RenderControl(htmlWriter);
    			textWriter.Flush();
    			dataStream.Seek(0, SeekOrigin.Begin);
    
    			using (StreamReader dataReader = new StreamReader(dataStream))
    			{
    				string htmlContent = dataReader.ReadToEnd();
    				Console.WriteLine(htmlContent);
    			}
    		}
    	}
    }

  5. #5

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: [2.0] Convert HtmlTable to HTML source code

    Yes, perfectly. Thanks!!!
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width