Fix missing HTML Tags using htmlagility pack

html html-agility-pack

Question

my input contain

<table border="0" align="center" width="100%">
<tr><td class="header">A
<td class="header">B
<td class="header"><b>C</b>
</tr>
</table>

the required output is

<table border="0" align="center" width="100%">
<tr><td class="header">A</td>
<td class="header">B</td>
<td class="header"><b>C</b></td>
</tr>
</table>

I tried the following reference

How to fix html tags(which is missing the <open> & <close> tags) with HTMLAgilityPack

the output i got

<table border="0" align="center" width="100%">
<tr><td class="header"></td>A
<td class="header"></td>B
<td class="header"></td><b>C</b>
</tr>
</table>

I am new to this HTML files...

Thanks in advance..

Accepted Answer

The Html Agility Pack has a special option available as a property on the HtmlDocument class, named OptionFixNestedTags for fixing this kind of HTML errors:

    static void Main(string[] args)
    {
        HtmlDocument doc = new HtmlDocument();
        doc.OptionFixNestedTags = true;
        doc.Load(YourFile);

        doc.Save(Console.Out);
    }

This will output:

<table border="0" align="center" width="100%">
<tr><td class="header">A
</td><td class="header">B
</td><td class="header"><b>C</b>
</td></tr>
</table>


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