我目前正在使用下面的代码来提取InnerText,但是,如果我被一堆html <--的注释掉的行卡住了,我如何使用下面的代码删除这些行?
HtmlWeb hwObject = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldocObject = hwObject.Load(htmlURL);
foreach (var script in htmldocObject.DocumentNode.Descendants("script").ToArray())
script.Remove();
HtmlNode body = htmldocObject.DocumentNode.SelectSingleNode("//body");
resultingHTML = body.InnerText.ToString();发布于 2012-02-02 10:33:56
This可能是一个更好的答案:
public static void RemoveComments(HtmlNode node)
{
foreach (var n in node.ChildNodes.ToArray())
RemoveComments(n);
if (node.NodeType == HtmlNodeType.Comment)
node.Remove();
}发布于 2012-02-03 05:59:29
只需按注释节点过滤节点,并对其调用remove。
var rootNode = doc.DocumentNode;
var query = rootNode.Descendants().OfType<HtmlCommentNode>().ToList();
foreach (var comment in query)
{
comment.Remove();
}https://stackoverflow.com/questions/9106177
复制相似问题