删除 EWS EmailMessage 正文中 HTML 的部分
Remove Portion of HTML in EWS EmailMessage Body
我想在通过 C#
中的 ResponseMessage
回复之前删除 EWS EmailMessage
.Body.Text
值的一部分 HTML
内容
要删除的内容如下,包含可点击和不可点击的html
按钮。
我看到我们不能声明我们自己的自定义标签,所以我无法通过定位自定义 html 标签来使用 string.replace
。
我可以知道我的任务是否有解决方法,例如将下面的内容放在占位符中等吗?
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" width="100%" style="width:100.0%">
<tbody>
<tr>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="30%" style="width:30.0%;background:#17202A;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white">ACTION: ADD
<o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="30%" style="width:30.0%;background:#17202A;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white">ACTION: MINUS
<o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="20%" style="width:20.0%;background:#3A69C2;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white"><a href="mailto:test.com?subject=[MULTIPLY];body=ACTION:%20MULTIPLY"><span style="color:white;background:#3A69C2;text-decoration:none">MULTIPLY
</span></a><o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="20%" style="width:20.0%;background:#3A69C2;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white"><a href="mailto:test.com?subject=[DIVIDE];body=ACTION:%20DIVIDE"><span style="color:white;background:#3A69C2;text-decoration:none">DIVIDE
</span></a><o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
您可以使用 html 标签的 Id 属性 https://www.w3schools.com/tags/att_id.asp 来识别您要删除的标签吗?
假设您要删除包含乘法和除法按钮的 table,并且在文档的其他任何地方都找不到它们,我们可以将它们与 XPath 结合使用来找到 table并将其删除。您可以使用 inbuild XMLDocument
class 但因为它 HTML 我建议使用 HTMLAgilityPack (可作为 nuget 包使用)来解析 HTML .
你最终会得到这样的结果:
//Create a HTMLAgilityPack Document
HtmlDocument doc = new HtmlDocument();
//Load the email body
doc.LoadHtml(EmailMessage.Body.Text);
//Select the ancestor table of the link we're interested in
HtmlNode node = doc.DocumentNode.SelectSingleNode("//a[@href='mailto:test.com?subject=[DIVIDE];body=ACTION:%20DIVIDE']//ancestor::table");
//Remove the table
node.Remove();
//Get the new email body
string newBody = doc.DocumentNode.InnerHtml;
您可能需要稍微调整一下才能到达那里,但希望这是一个好的开始。
我想在通过 C#
ResponseMessage
回复之前删除 EWS EmailMessage
.Body.Text
值的一部分 HTML
内容
要删除的内容如下,包含可点击和不可点击的html
按钮。
我看到我们不能声明我们自己的自定义标签,所以我无法通过定位自定义 html 标签来使用 string.replace
。
我可以知道我的任务是否有解决方法,例如将下面的内容放在占位符中等吗?
<table class="MsoNormalTable" border="0" cellspacing="0" cellpadding="0" width="100%" style="width:100.0%">
<tbody>
<tr>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="30%" style="width:30.0%;background:#17202A;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white">ACTION: ADD
<o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="30%" style="width:30.0%;background:#17202A;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white">ACTION: MINUS
<o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="20%" style="width:20.0%;background:#3A69C2;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white"><a href="mailto:test.com?subject=[MULTIPLY];body=ACTION:%20MULTIPLY"><span style="color:white;background:#3A69C2;text-decoration:none">MULTIPLY
</span></a><o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
<td width="20%" style="width:20.0%;background:#3A69C2;padding:4.5pt 4.5pt 4.5pt 4.5pt;display:inline-block">
<p class="MsoNormal" align="center" style="text-align:center"><b><span style="font-size:11.5pt;font-family:"Helvetica",sans-serif;color:white"><a href="mailto:test.com?subject=[DIVIDE];body=ACTION:%20DIVIDE"><span style="color:white;background:#3A69C2;text-decoration:none">DIVIDE
</span></a><o:p></o:p></span></b></p>
</td>
<td style="padding:0in 0in 0in 0in">
<p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New Roman",serif"> <o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
您可以使用 html 标签的 Id 属性 https://www.w3schools.com/tags/att_id.asp 来识别您要删除的标签吗?
假设您要删除包含乘法和除法按钮的 table,并且在文档的其他任何地方都找不到它们,我们可以将它们与 XPath 结合使用来找到 table并将其删除。您可以使用 inbuild XMLDocument
class 但因为它 HTML 我建议使用 HTMLAgilityPack (可作为 nuget 包使用)来解析 HTML .
你最终会得到这样的结果:
//Create a HTMLAgilityPack Document
HtmlDocument doc = new HtmlDocument();
//Load the email body
doc.LoadHtml(EmailMessage.Body.Text);
//Select the ancestor table of the link we're interested in
HtmlNode node = doc.DocumentNode.SelectSingleNode("//a[@href='mailto:test.com?subject=[DIVIDE];body=ACTION:%20DIVIDE']//ancestor::table");
//Remove the table
node.Remove();
//Get the new email body
string newBody = doc.DocumentNode.InnerHtml;
您可能需要稍微调整一下才能到达那里,但希望这是一个好的开始。