How to fix html tags(which is missing the & tags) with HTMLAgilityPack

c# dom html-agility-pack

Question

I have an html with <div><h1> hello Hi</div> <div>hi </p></div>

Required Output : <div><h1> hello </h1></div> <div><p>hi </p></div>

Using HTML agility pack is it possible to fix this kind of similar issues with missing closing and opening tags?

Accepted Answer

The library isn't intelligent enough to create the opening p where you put it, but it's intelligent enough to create the missing h1. And in general, it creates valid HTML always, but not always the one you would expect.

So this code:

        HtmlDocument doc = new HtmlDocument();
        doc.Load(yourhtml);
        doc.Save(Console.Out);

will dump this:

<div><h1> hello Hi</h1></div> <div>hi <p></div>

Which is not what you want, but is valid HTML. You can also add a little trick like this:

        HtmlNode.ElementsFlags["p"] = HtmlElementFlag.Closed;
        HtmlDocument doc = new HtmlDocument();
        doc.Load(yourhtml);
        doc.Save(Console.Out);

that will dump this:

<div><h1> hello Hi</h1></div> <div>hi <p></p></div>

Popular Answer

When doing HtmlAgilityPack.HtmlDocument.LoadHTML(yourhtml) HTMLAgilityPack will automatically fix the tags for you, and then you can access those tags using: HtmlAgilityPack.HtmlDocument.DocumentNode.OuterHTML



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