HtmlAgilityPack produces missing closing tags in OuterHtml

c# html html-agility-pack

Question

I am parsing and modifying HTML content using HtmlAgilityPack. The DocumentNode.OuterHtml seems to provide the needed closing tags, however.

I am now just parsing and obtaining the OuterHtml (without any alteration) in order to narrow down the problem:

var document = new HtmlDocument();
document.LoadHtml(myHtml);
result = document.DocumentNode.OuterHtml;

Original: (myHtml)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="X-UA-Compatible" content="IE=Edge" /><title>
     MyTitle
</title>

OutputHtml: Zzz-23-Zzz (result)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="X-UA-Compatible" content="IE=Edge"><title>
    MyTitle
</title>

All input and image elements are similarly left blank. (Kindly refrain from responding that there shouldn't be an issue. Although it shouldn't be, it is.) Chrome is unable to display the website properly. Go on reading.

What is stranger:

Original: (myHtml)

    <option value="10">Afrikaans</option>
    <option value="11">Albanian</option>
    <option value="12">Arabic</option>
    <option value="13">Armenian</option>
    <option value="14">Azerbaijani</option>
    <option value="15">Basque</option>

OutputHtml: Zzz-46-Zzz (result)

    <option value="10">Afrikaans
    <option value="11">Albanian
    <option value="12">Arabic
    <option value="13">Armenian

using the HtmlAgilityPack 1.4.9 NuGet package, which has the following ID:

1
1
2/3/2016 2:25:41 PM

Accepted Answer

When you load the document, you have a number of settings to choose from.

OptionAutoCloseOnEnd

Specifies whether closure for open nodes must occur at the end or earlier in the document. The way that browsers render the page may be altered by setting this to true.

document = new HtmlDocument();
document.OptionAutoCloseOnEnd = true;
document.LoadHtml(content);

Worth reading related sources are:

Option End Tags Are Removed by HtmlAgilityPack

HTMLAgilityPack does not close image tags

4
5/23/2017 12:30:59 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow