Drakkin
Drakkin

Reputation: 908

Remove node of single child parent in html agility pack

I'm using Html Agility Pack (1.4.9.5) to remove a node within a specified class:

var document = new HtmlDocument();
document.LoadHtml("<p><div class=\"remove-it\"></div></p>");

var nodesToRemove = document.QuerySelectorAll(".remove-it");
if (nodesToRemove != null)
{
    foreach (var node in nodesToRemove)
    {
        node.Remove();
    }
}

var res = document.DocumentNode.OuterHtml;

The problem is that at the end res is equal to:

<p>

but it should be:

<p></p>

How can I fix this?

Upvotes: 0

Views: 437

Answers (1)

Zroq
Zroq

Reputation: 8382

Almost there! You are missing HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed; before document.LoadHtml("<p><div class=\"remove-it\"></div></p>");.

What that does is that the p element will be automatically closed when parsing the document.

Upvotes: 1

Related Questions