0

我正在从系统中获取这个 Html 字符串

<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>&nbsp;
    <br>
 </p>
 <p style="font-size: 14px; font-weight: 400; text-align: justify;">  
   <br>
 </p>
</h1>

我在这个字符串中有两个 h1 标签。我想删除使用“table”标签的“h1”标签。

如何以编程方式删除它?

4

1 回答 1

0

您可以使用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>&nbsp;<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>&nbsp;
    <br>
 </p>
 <p style="font-size: 14px; font-weight: 400; text-align: justify;">  
   <br>
 </p>
于 2020-06-08T06:29:47.963 回答