How to delete a html tag alone but not inner html or children tag using HTMLAgilityPack?

我与影子孤独终老i 提交于 2019-12-11 13:13:01

问题


I need to delete an html tag say <tbody> in the following code,

<TABLE>
  <TBODY>
  <TR>    
    <TD></TD>
    <TD></TD>
    <TD></TD></TR>
  <TR>    
    <TD valign="bottom"></TD>
    <TD valign="bottom"></TD>
    <TD valign="bottom"></TD></TR>
  </TBODY>
</TABLE>

I'm using,

      var document = new HtmlDocument();
      document.LoadHtml(<URL>);
      if (document.DocumentNode.SelectSingleNode("//tbody") != null)
                {
                    document.DocumentNode.SelectSingleNode("//tbody").Remove();
                }

But its deleting the entire block instead of just alone :(

Appreciate your help & time :)


回答1:


var tbody = document.DocumentNode.SelectSingleNode("//tbody");
tbody.ParentNode.RemoveChild(tbody, keepGrandChildren: true);

OUTPUT:

<table>

  <tr>    
    <td valign="bottom"></td>
    <td valign="bottom"></td>
    <td valign="bottom"></td></tr>
  <tr>    
    <td></td>
    <td></td>
    <td></td></tr>

</table>



回答2:


The inner html is an integral part of the tag, that's why the inner html is also getting deleted.

What you need to do is replace the <tbody> tag by the inner html of <tbody>, in your case, something like this (i did not check if this code works, but you get the idea):

document.DocumentNode.SelectSingleNode("//table").innerHTML = document.DocumentNode.SelectSingleNode("//tbody").innerHTML;



回答3:


If you give your tags an id, you should be able to access the element by id. This will make it super easy to delete.



来源:https://stackoverflow.com/questions/18467498/how-to-delete-a-html-tag-alone-but-not-inner-html-or-children-tag-using-htmlagil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!