编程语言
首页 > 编程语言> > c# – Html Agility Pack – 删除元素,但不删除innerHtml

c# – Html Agility Pack – 删除元素,但不删除innerHtml

作者:互联网

我可以通过note.Remove()来轻松删除元素:

HtmlDocument html = new HtmlDocument();

html.Load(Server.MapPath(@"~\Site\themes\default\index.cshtml"));

foreach (var item in html.DocumentNode.SelectNodes("//removeMe"))
{
    item.Remove();
}

但这也删除了innerHtml.
如果我只想删除标签并保留innerHtml怎么办?

例:

<ul>
    <removeMe>
        <li>
            <a href="#">Keep me</a>
        </li>
    </removeMe>
</ul>

任何帮助,将不胜感激 :)

解决方法:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var node = doc.DocumentNode.SelectSingleNode("//removeme");
node.ParentNode.RemoveChild(node, true);

标签:c,html-agility-pack,html
来源: https://codeday.me/bug/20190711/1435660.html