您可以使用HtmlAgilityPack
:
var content = @"<h1>PDF Attachment</h1><h1 style=""color: rgb(51, 51, 51); text-align: center;""><p style=""font-size: 14px; font-weight: 400; text-align: justify;"">Your service detail are following</p><p style=""font-size: 14px; font-weight: 400; text-align: justify;""><table><tr><td></td></tr></table> <br></p><p style=""font-size: 14px; font-weight: 400; text-align: justify;""><br></p></h1>";
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(content);
var h1NeedsToRemove = htmlDoc.DocumentNode.SelectNodes("/h1").Where(i => i.ChildNodes.Any(c => c.Name == "table")).FirstOrDefault();
var childNodesOfH1 = h1NeedsToRemove.ChildNodes;
h1NeedsToRemove.Remove();
htmlDoc.DocumentNode.AppendChildren(childNodesOfH1);
它会给你想要的输出:
<h1>PDF Attachment</h1>
<p style="font-size: 14px; font-weight: 400; text-align: justify;">Your service detail are following</p><p style="font-size: 14px; font-weight: 400; text-align: justify;">
<table>
<tr><td></td></tr>
</table>
<br>
</p>
<p style="font-size: 14px; font-weight: 400; text-align: justify;">
<br>
</p>