HtmlAgilityPack在OuterHtml中生成缺少的结束标记 [英] HtmlAgilityPack produces missing closing tags in OuterHtml

查看:191
本文介绍了HtmlAgilityPack在OuterHtml中生成缺少的结束标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HtmlAgilityPack解析和处理html文本.但是,似乎DocumentNode.OuterHtml提供了缺少的结束标记.

I am using HtmlAgilityPack to parse and manipulate html text. However it seems the DocumentNode.OuterHtml gives missing closing tags.

要想找出问题,我现在什么也没做,只是解析并获取OuterHtml(无需任何操作):

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

原文:(myHtml)

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 :(结果)请注意,元元素未关闭

OutputHtml: (result) Notice that meta element is not closed

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

类似地,所有输入和img元素均保持打开状态. (请不要回答这应该不是问题.应该是,但是应该是.)Chrome无法正确呈现页面.继续阅读.

Similarly all input and img elements are leaved open. (Please do not answer that it should not be a problem. Well it should not be, but it is.) Chrome can not render the page correctly. Keep reading.

更奇怪的是:

原文:(myHtml)

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

OutputHtml: (result) Notice that that complete explicit closing tags are missing

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

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

Using HtmlAgilitPack latest NuGet package: id="HtmlAgilityPack" version="1.4.9"

推荐答案

加载文档时可以设置几个选项.

There are several options that you can set when you are loading the document.

OptionAutoCloseOnEnd

OptionAutoCloseOnEnd

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

Defines if closing for non closed nodes must be done at the end or directly in the document. Setting this to true can actually change how browsers render the page.

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

相关资料值得阅读:

HtmlAgilityPack丢弃选项结束标签

图像标记未使用HTMLAgilityPack关闭

这篇关于HtmlAgilityPack在OuterHtml中生成缺少的结束标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆