HtmlAgilityPack 在 OuterHtml 中生成缺少的结束标记

HtmlAgilityPack produces missing closing tags in OuterHtml

我正在使用 HtmlAgilityPack 来解析和操作 html 文本。然而,DocumentNode.OuterHtml 似乎缺少结束标记。

现在为了隔离问题,我什么都不做,只是解析并获取 OuterHtml(无操作):

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

原文:(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:(结果)注意元元素没有关闭

<!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>

同样,所有 input 和 img 元素都保持打开状态。 (请不要回答这应该不是问题。它不应该是,但它是。) Chrome 无法正确呈现页面。继续阅读。

更奇怪的是:

原文:(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:(结果)请注意缺少完整的显式结束标记

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

使用 HtmlAgilitPack 最新的 NuGet 包:id="HtmlAgilityPack" version="1.4.9"

您可以在加载文档时设置多个选项。

OptionAutoCloseOnEnd

定义是否必须在文档末尾或直接在文档中完成非封闭节点的封闭。将此设置为 true 实际上可以改变浏览器呈现页面的方式。

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

值得一读的相关资料:

HtmlAgilityPack Drops Option End Tags

Image tag not closing with HTMLAgilityPack