HtmlAgilityPack LoadHtml - Issue with empty P tags

c# html-agility-pack

Question

I am trying to load simple html:

var html = "<div>something<p></p></div>";

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

After loading this HTML doc.InnerHtml outputs this:

<div>something<p></div>

So as you can see it loses closing P tag and I got messed HTML.

I cannot find any solution for this, can anyone help me please.

Accepted Answer

The OptionWriteEmptyNodes flag is what you're looking for:

Defines if empty nodes must be written as closed during output.

And in your case:

doc.OptionWriteEmptyNodes = true;

Yields:

<div>something<p /></div>


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why