HTMLAgilityPack-删除节点,但不剥离内部文本 [英] HTMLAgilityPack - Remove Node with out stripping the inner text
本文介绍了HTMLAgilityPack-删除节点,但不剥离内部文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的html内容是
<a href="#asdf">asdf</a>
<H5 align="left"><A href="#d570525d497.htm#toc">Table
of Contents</A><br></H5>
我正在使用HTML Agility Pack加载html.我想找到<a>
节点并删除该节点而不删除其内部文本,如下所述
I'm using HTML Agility Pack to load the html. I want to find <a>
nodes and remove the node without removing its inner text as mentioned below
asdf
<H5 align="left">Table
of Contents<br></H5>
我正在使用以下代码,
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlPage);
var Nodes = htmlDocument.DocumentNode.SelectNodes("//a");
foreach (var Node in Nodes)
{
Node.InnerText.Trim();
}
它不起作用.代码有问题吗?
It's not working. Something wrong with the code?
推荐答案
在保留大孩子的同时,从父节点删除节点
Remove the node from parent while keeping the grand children
foreach (var Node in Nodes)
{
Node.ParentNode.RemoveChild(Node, true); //<-- keepGrandChildren
}
var newhtml = htmlDocument.DocumentNode.OuterHtml;
输出:
asdf
<h5 align="left">Table
of Contents<br></h5>
这篇关于HTMLAgilityPack-删除节点,但不剥离内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文