Remove TR Tag By ID From Email Template HTML File

We all use HTML templates while sending emails. And sometimes, we need to remove some content from that file at runtime, especially when we need to remove TR tag from table HTML by Id. Previously, it was a tedious task for me. But after a lot of Google search, finally I found the solution of how to remove specific TR tags from table by Id at runtime.
RemoveTrTag() Function
  1. public void RemoveTrTag()  
  2. {  
  3.      var filePath = Server.MapPath("~/JobChunkEmailTemplate.html");  
  4.      StringBuilder sbMailTemplate = new StringBuilder();  
  5.      using (StreamReader reader = new StreamReader(filePath, Encoding.Default))  
  6.      {  
  7.            sbMailTemplate.Append(reader.ReadToEnd());  
  8.      }  
  9.      string pattern = "<tr id=\"GoogleAdwordsSectionId\".+?</tr>";  
  10.      Regex r = new Regex(pattern, RegexOptions.Singleline);  
  11.      string result = r.Replace(sbMailTemplate.ToString(), "");  
  12.      sbMailTemplate.Replace(sbMailTemplate.ToString(), result);  
  13. }  
Do not forget to add the below namespaces in your code.
  1. using System.IO;  
  2. using System.Text;  
  3. using System.Text.RegularExpressions;  
HTML file Contents 
  1. <body>  
  2. <form id="form1">  
  3. <table id="EmailTable" style="width:100%;border: 1px solid black;border-collapse: collapse;">  
  4.     <tr id="GASectionId" style="border-width:1px;border-style:solid;#GA#">  
  5.          <td style="width:10%;border: 1px solid black;">Account</td>  
  6.          <td style="width:15%;border: 1px solid black;">ProjectId</td>  
  7.          <td style="width:15%;border: 1px solid black;">Extraction Date</td>  
  8.          <td style="width:50%;border: 1px solid black;">Failure Reason</td>  
  9.          <td style="width:10%;border: 1px solid black;">Component</td>  
  10.      </tr>  
  11.      <tr id="GoogleAdwordsSectionId" style="border-width:1px;border-style:solid;#GoggleAds#">  
  12.          <td style="width:15%;border: 1px solid black;">CustomerId / AccountId</td>  
  13.          <td style="width:10%;border: 1px solid black;">LocationId</td>  
  14.          <td style="width:15%;border: 1px solid black;">Extraction Date</td>  
  15.          <td style="width:50%;border: 1px solid black;">Failure Reason</td>  
  16.          <td style="width:10%;border: 1px solid black;">Component</td>  
  17.      </tr>  
  18.     {Body}  
  19. </table>  
  20. </form>  
  21. </body>  
In my case, tr tag Id is GoogleAdwordsSectionId. Put the Id in reg expression and try with your example. You can also find a demo example in source code.

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now