如何使用HTMLAgilitypack循环浏览HTML的每个节点并删除某些节点

本文关键字:节点 删除 HTMLAgilitypack 何使用 循环 浏览 HTML | 更新日期: 2023-09-27 18:29:13

我需要识别每个节点并删除某些节点,如Plegend等。。需要使用HTMLAgilityPack、循环浏览以下html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><META content="IE=5.0000" http-equiv="X-UA-Compatible">
<META http-equiv="Content-Type" content="text/html; charset=windows-1252">
</HEAD>
<BODY bgcolor="white"><text><TITLE>ABCD</TITLE> 
<P style="page-break-before: always;">
<HR width="100%" size="3" align="CENTER" style="color: rgb(153, 153, 153);">
<fieldset>
    <legend>Personalia:</legend>
    Name: <input type="text"><br>
    Email: <input type="text"><br>
    Date of birth: <input type="text">
  </fieldset>
<P style="margin-top: 0px; margin-bottom: 0px;"><FONT size="1">&nbsp;</FONT></P>
<P align="center" style="margin-top: 0px; margin-bottom: 0px;"><FONT size="2" 
style="font-family: Times New Roman;">B-17 </FONT></P></text>
</BODY></HTML>

如何使用HTMLAgilitypack循环浏览HTML的每个节点并删除某些节点

我只是举一个例子,试试

                String content = "Your Html page source as string";
                HtmlNode.ElementsFlags.Remove("form");
                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(content);
                // Pass the name of the tag you want to remove 
                DeleteTagByName("tagname",doc);
                public void DeleteTagByName(string name, HtmlDocument HtmlDocument)
                 {
                     HtmlDocument.DocumentNode.SelectSingleNode("//input[@name='" + name + "']").Remove();
                }