Html Agility Pack - Remove element, but not innerHtml

c# html html-agility-pack

Question

With simply a comment, I can quickly delete the component. similar to this:

HtmlDocument html = new HtmlDocument();

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

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

However, that also deletes the innerHtml. What if all I want to do is preserve the innerHtml but leave the tag alone?

Example:

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

I would appreciate any assistance:)

1
4
3/22/2013 9:25:22 AM

Accepted Answer

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

var node = doc.DocumentNode.SelectSingleNode("//removeme");
node.ParentNode.RemoveChild(node, true);
22
8/23/2012 1:58:54 PM

Popular Answer

For users who could have text within the element they are seeking to delete, the bool KeepGrandChildren implementation has an issue. The text that was included inside the removeme tag will likewise be deleted. For instance<removeme>text<p>more text</p></removeme> will develop<p>more text</p>

Do this:

private static void RemoveElementKeepText(HtmlNode node)
    {
        //node.ParentNode.RemoveChild(node, true);
        HtmlNode parent = node.ParentNode;
        HtmlNode prev = node.PreviousSibling;
        HtmlNode next = node.NextSibling;

        foreach (HtmlNode child in node.ChildNodes)
        {
            if (prev != null)
                parent.InsertAfter(child, prev);
            else if (next != null)
                parent.InsertBefore(child, next);
            else
                parent.AppendChild(child);

        }
        node.Remove();
    }


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow