[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...
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;
}
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...
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);
}
}
}
}
Re: [2.0] Convert HtmlTable to HTML source code
Yes, perfectly. Thanks!!!