I want to do, Find the html element from HTML string and replace with HTML table, please help to complete my task. thanks in advance
I have attach sample HTML and my code behind code, here I want to remove tag with id sample_id in the HTML and add a table to that place in HTML string
<div class="row">
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.
</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more »</a></p>
</div>
<span class="sample_class" id="sample_id"></span>
</div>
public string ReplacePlaceHolder(string value)
{
string HTMLToConvert = "";
StringWriter myWriter = new StringWriter();
// Decode the encoded string.
HTMLToConvert = HttpUtility.UrlDecode(value).ToString();
//HtmlDocument doc = new HtmlDocument();
//doc.LoadHtml(HTMLToConvert);
//var nodes = doc.DocumentNode.SelectSingleNode("//span[@class='placeholder']");
string generatedHTMLtable = GenerateHTMLTable();
StringBuilder builder = new StringBuilder(HTMLToConvert);
builder.Replace("<span class='sample_class' id='sample_id'></span>", generatedHTMLtable);
StringReader sr = new StringReader(builder.ToString());
return sr.ToString();
}
//Sample method for generating string of HTML table
public string GenerateHTMLTable()
{
string tableHtml = "";
DataSet ds = new DataSet();
DataTable dt = new DataTable("FirstTable");
dt.Columns.Add(new DataColumn("UserID", typeof(int)));
dt.Columns.Add(new DataColumn("AccountID", typeof(int)));
dt.Columns.Add(new DataColumn("Code", typeof(string)));
dt.Columns.Add(new DataColumn("AccountName", typeof(string)));
dt.Columns.Add(new DataColumn("GroupCode", typeof(string)));
for (int i = 0; i < 6; i++)
{
DataRow dr = dt.NewRow();
dr["UserID"] = i;
dr["AccountID"] = i + 1;
dr["Code"] = "COD" + i;
dr["AccountName"] = "Account" + i;
dr["GroupCode"] = "GRP" + i;
dt.Rows.Add(dr);
}
ds.Tables.Add(dt);
tableHtml += "<table>";
tableHtml += "<tr><th>UserID</th><th>AccountID</th><th>Code</th><th>AccountName</th><th>GroupCode</th></tr>";
foreach (DataRow drAccount in dt.Rows)
{
tableHtml += "<tr><td>" + drAccount["UserID"] + "</td><td>" + drAccount["AccountID"] + "</td><td>" + drAccount["Code"] + "</td><td>" + drAccount["AccountName"] + "</td><td>" + drAccount["GroupCode"] + "</td></tr>";
}
tableHtml += "</table>";
return tableHtml;
}
<span class="sample_class" id="sample_id"></span>with whatever you want