HTML Agility Pack tr insertion at the end of a table

c# html-agility-pack

Question

I have this table

<div id="ConversationDIv" runat="server">
   <table border="1" id="tbl">
       <tr>
          <td>blah blah</td>
       </tr>
   </table>
</div>
<asp:Button id="Insert" onCLick="Insert_Click" Text="addNew" ></asp:Button>

c# :

protected void Insert_Click(object sender,EventArgs e)
{
    var html = new HtmlAgilityPack.HtmlDocument();

    html.LoadHtml(ConversationDIv.InnerHtml);

    var table = html.DocumentNode.SelectNodes("table").FirstOrDefault();
    // how can I add a new row to table ?
}

I want to add a new row to the table how can I do this ?

Popular Answer

You can use SelectSingleNode for selecting table. And use HtmlNode.CreateNode to create node being appended from html string:

var table = html.DocumentNode.SelectSingleNode("//table");
table.AppendChild(HtmlNode.CreateNode("<tr></tr>"));



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why