首页 > 解决方案 > HtmlAgilityPack 删除节点(头)不起作用

问题描述

想要删除一个完整的thead(包括th's)。为什么这不起作用?我尝试了其他标签,没有任何反应,结果文本是一样的。就像没有变化一样。

<table>
   <thead>
      <tr>
        <th>Hora</th>
        <th>Estado</th>
        <th>Motivo</th>
         <th>Local</th>
         <th>Recetor</th>
       </tr>
     </thead>
 </table>

c# 代码

doc.LoadHtml("<table><thead><th>Hora</th><th>Estado</th><th>Motivo</th><th>Local</th><th>Recetor</th></thead></table>");

var nodes = doc.DocumentNode.SelectNodes("//thead").ToList();

foreach (var node in nodes) {
  node.Remove();
}

txtResults.Text=doc.Text;

标签: c#html-agility-pack

解决方案


HtmlDocument.Text属性有非常不清楚的描述:

HtmlDocument 文本。如果你修改它要小心。

从观察到的行为看来,当您修改 html 文档时,此属性未更新。所以doc.DocumentNode.OuterHtml改用。


更新:从ParsedText属性实现看起来Text应该保存原始未修改的解析文本:

public string ParsedText
{
    get { return Text; }
}

但这甚至不是一个只读属性——它是一个任何人都可以随时修改的公共字段。所以我不会相信HtmlDocument.Text它的描述所说的。


推荐阅读