c# - 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;
解决方案
HtmlDocument.Text
属性有非常不清楚的描述:
HtmlDocument 文本。如果你修改它要小心。
从观察到的行为看来,当您修改 html 文档时,此属性未更新。所以doc.DocumentNode.OuterHtml
改用。
更新:从ParsedText
属性实现看起来Text
应该保存原始未修改的解析文本:
public string ParsedText
{
get { return Text; }
}
但这甚至不是一个只读属性——它是一个任何人都可以随时修改的公共字段。所以我不会相信HtmlDocument.Text
它的描述所说的。
推荐阅读
- c# - 我如何发送消息选项卡和向上箭头发送到 winform?C#
- sql - SQL - 一行中有多个记录
- python - 通过 PRESTO 连接器选择随机数据样本
- python - urllib.error.URLError:
代码:
import os import tarfile from urllib.request import urlretrieve
- python - 元编程 - 更改 SIMD 计算的表示
- javascript - 当我修改取决于我的父状态的变量时,我不想修改初始状态
- c# - 在 Entity Framework Core 中出现错误
- python - 我可以在函数内多次返回一个变量吗?
- node.js - Node Sequelize Postgres - BulkCreate 忽略自定义字段的重复值
- bash - 如何使用 Windows 上的 Git Bash 和脚本命令一键部署我的网站