HTMLAgilityPack don't preserves original empty tags

asp.net c# html-agility-pack

Question

If i have some empty tags like this

<td  width="15px"/>

Agility pack fixes them to be like

<td  width="15px"></td>

Is anything possible to do to override this behavior ?

Accepted Answer

Try this before saving:

if (HtmlNode.ElementsFlags.ContainsKey("td"))
{
    HtmlNode.ElementsFlags["td"] = HtmlElementFlag.Empty | HtmlElementFlag.Closed;
}
else
{
    HtmlNode.ElementsFlags.Add("td", HtmlElementFlag.Empty | HtmlElementFlag.Closed);
}

This changes the behavior for all td elements which may not be what you want. I don't know of a way to accomplish this per-node.


Popular Answer

Set the OptionWriteEmptyNodes property to true on your HtmlDocument.




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