我在尝试删除具有特定 ID 的 div 及其使用 HTML Agility 包的子项时遇到困难。我确信我只是缺少一个配置选项,但现在是星期五,我正在苦苦挣扎。
简化的 HTML 运行:
<html><head></head><body><div id='wrapper'><div id='functionBar'><div id='search'></div></div></div></body></html>
这就是我所知道的。敏捷包抛出的错误显示找不到div结构:
<div id='functionBar'></div>
这是到目前为止的代码(取自 Stackoverflow....)
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
// There are various options, set as needed
//htmlDoc.OptionFixNestedTags = true;
// filePath is a path to a file containing the html
htmlDoc.LoadHtml(Html);
string output = string.Empty;
// ParseErrors is an ArrayList containing any errors from the Load statement
if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count > 0)
{
// Handle any parse errors as required
}
else
{
if (htmlDoc.DocumentNode != null)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
if (bodyNode != null)
{
HtmlAgilityPack.HtmlNode functionBarNode = bodyNode.SelectSingleNode ("//div[@id='functionBar']");
bodyNode.RemoveChild(functionBarNode,false);
output = bodyNode.InnerHtml;
}
}
}
请您参考如下方法:
bodyNode.RemoveChild(functionBarNode,false);
但是 functionBarNode 不是 bodyNode 的子节点。
functionBarNode.ParentNode.RemoveChild(functionBarNode, false) 怎么样? (并且忘记关于查找 bodyNode 的部分。)
