简体   繁体   English

如何使用 Html Agility Pack 删除特定标签内的跨度?

[英]How to remove span inside specific tags using Html Agility Pack?

I have a sample html below which I want to remove all span tags inside the comment tags using html agility pack我有一个示例 html 下面我想使用 html 敏捷包删除评论标签内的所有跨度标签

             <comment id="e096f3920ecbd8378f2b77b9608588434" type="start"></comment>
                <span style="color:hsl(0,0%,0%);">
                       <span style="color:hsl(0,0%,0%);">
                            Microsoft
                        </span>
                </span>
             <comment id="e096f3920ecbd8378f2b77b9608588434" type="end"></comment>

This html is auto generated by ckeditor which the comment tag has type ( start and end ).此 html 由 ckeditor 自动生成,其中注释标签具有类型( startend )。 Is it possible to remove some tags inside the comment tag start to end using html agility pack?是否可以使用 html 敏捷包从开始到结束的注释标签中删除一些标签?

Updated Question(10/24/19)更新问题(19 年 10 月 24 日)

Complete HTML(by ckeditor)完整的 HTML(由 ckeditor 提供)

<p>
   <span style="color:hsl(0,0%,0%);">
      <span style="color:hsl(0,0%,0%);">
         <span style="color:hsl(0,0%,0%);">
            <span style="color:hsl(0,0%,0%);">
               <span style="color:hsl(0,0%,0%);">
                  <span style="color:hsl(0,0%,0%);">
                     <span style="color:hsl(0,0%,0%);">
                        <comment id="ef7492a2e61d2666914b6a947aef5a6d6" type="start"></comment>
                        <span style="color:hsl(0,0%,0%);">This&nbsp;</span>
                     </span>
                  </span>
               </span>
            </span>
         </span>
      </span>
      <comment id="e042cfd52178aaa260f79e1accd66441c" type="start"></comment>
      Microsoft&nbsp;
      <comment id="e042cfd52178aaa260f79e1accd66441c" type="end"></comment>
      <span style="color:hsl(0,0%,0%);">
         <span style="color:hsl(0,0%,0%);">
            <span style="color:hsl(0,0%,0%);">
               <span style="color:hsl(0,0%,0%);">
                  <span style="color:hsl(0,0%,0%);">
                     <span style="color:hsl(0,0%,0%);">
                        <span style="color:hsl(0,0%,0%);">Enterprise</span>
                        <comment id="ef7492a2e61d2666914b6a947aef5a6d6" type="end"></comment>
                        <span style="color:hsl(0,0%,0%);"> Agreement is entered into between the entities identified on the signature form.</span>
                     </span>
                  </span>
               </span>
            </span>
         </span>
      </span>
   </span>
</p>

I want to remove all the span inside the comment start id (ef7492a2e61d2666914b6a947aef5a6d6) and comment end id (ef7492a2e61d2666914b6a947aef5a6d6)我想删除评论开始 id (ef7492a2e61d2666914b6a947aef5a6d6) 和评论结束 id (ef7492a2e61d2666914b6a947aef5a6d6) 中的所有跨度

You can use Xpath query to select the " start " comments.您可以使用Xpath查询到 select 的“开始”注释。 After it, you need to loop over the sibling elements until you reach the " end " type of comment.之后,您需要遍历兄弟元素,直到到达“ end ”类型的注释。

var nodesToRemove = new List<HtmlAgilityPack.HtmlNode>();
var startNodes = doc.DocumentNode.SelectNodes("//comment[@type='start']");
foreach (var startNode in startNodes)
{
    var node = startNode.NextSibling;

    while (node != null)
    {
        var attribute = node.Attributes["type"];

        // found the "end" type of comment.
        if(attribute != null && attribute.Value == "end")
            break;

        if (node.Name == "span")
            nodesToRemove.Add(node);

        node = node.NextSibling;
    }
}

foreach (var node in nodesToRemove)
    node.Remove();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM