HTML敏捷包删除儿童 [英] html agility pack remove children
问题描述
我有困难,试图删除一个div与特定的ID,并使用HTML敏捷包它的孩子。 。我相信我只是缺少一个配置选项,但其周五和我挣扎
简化的HTML运行:
< HTML>< HEAD>< /头><身体GT;< DIV ID ='包装'>< DIV ID ='functionBar >< DIV ID ='搜索'>< / DIV>< / DIV>< / DIV>< /身体GT;< / HTML>
这是据我已经得到。由敏捷包抛出的错误显示它无法找到一个div结构:
< DIV ID ='functionBar'>< ; / DIV>
下面是到目前为止的代码
$ B $(从....#1所) b HtmlAgilityPack.HtmlDocument HTMLDOC =新HtmlAgilityPack.HtmlDocument();
//有多种选择,根据需要设置
//htmlDoc.OptionFixNestedTags = TRUE;
//文件路径是包含HTML
htmlDoc.LoadHtml(HTML)的文件的路径;
字符串输出=的String.Empty;
// ParseErrors是包含从Load声明
如果(htmlDoc.ParseErrors = NULL&放!;&安培; htmlDoc.ParseErrors.Count大于0)的任何错误一个ArrayList
{
//处理任何解析错误所需
}
,否则
{
如果(htmlDoc.DocumentNode!= NULL)
{
HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode(//体);
如果(!bodyNode = NULL)
{
HtmlAgilityPack.HtmlNode functionBarNode = bodyNode.SelectSingleNode(// DIV [@ ID ='functionBar']);
bodyNode.RemoveChild(functionBarNode,FALSE);
输出= bodyNode.InnerHtml;
}
}
}
bodyNode.RemoveChild(functionBarNode,FALSE);
块引用>
但functionBarNode不是一个孩子bodyNode。
如何
functionBarNode.ParentNode.RemoveChild(functionBarNode,FALSE)
? (而忘记寻找bodyNode位。)I'm having difficulty trying to remove a div with a particular ID, and its children using the HTML Agility pack. I am sure I'm just missing a config option, but its Friday and I'm struggling.
The simplified HTML runs:
<html><head></head><body><div id='wrapper'><div id='functionBar'><div id='search'></div></div></div></body></html>
This is as far as I have got. The error thrown by the agility pack shows it cannot find a div structure:
<div id='functionBar'></div>
Here's the code so far (taken from 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);
But functionBarNode is not a child of bodyNode.
How about
functionBarNode.ParentNode.RemoveChild(functionBarNode, false)
? (And forget the bit about finding bodyNode.)这篇关于HTML敏捷包删除儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!