我最近开始尝试 HtmlAgilityPack。我不熟悉它的所有选项,因此我认为我做错了。
我有一个包含以下内容的字符串:
string s = "<span style=\"color: #0000FF;\"><</span>";
你看,在我的范围内,我有一个“小于”符号。
我使用以下代码处理该字符串:
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(s);
但是当我像这样快速而肮脏地查看跨度时:
htmlDocument.DocumentNode.ChildNodes[0].InnerHtml
我看到跨度是空的。
我需要设置什么选项来保持“小于”符号。我已经尝试过这个:
htmlDocument.OptionAutoCloseOnEnd = false;
htmlDocument.OptionCheckSyntax = false;
htmlDocument.OptionFixNestedTags = false;
但没有成功。
我知道这是无效的 HTML。我用它来修复无效的 HTML 并在“小于”符号上使用 HTMLEncode
请指引我正确的方向。提前致谢
Html Agility Pack 将此检测为错误并为其创建一个 HtmlParseError 实例。您可以使用 HtmlDocument 类的 ParseErrors 读取所有错误。所以,如果你运行这段代码:
string s = "<span style=\"color: #0000FF;\"><</span>";
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(s);
doc.Save(Console.Out);
Console.WriteLine();
Console.WriteLine();
foreach (HtmlParseError err in doc.ParseErrors)
{
Console.WriteLine("Error");
Console.WriteLine(" code=" + err.Code);
Console.WriteLine(" reason=" + err.Reason);
Console.WriteLine(" text=" + err.SourceText);
Console.WriteLine(" line=" + err.Line);
Console.WriteLine(" pos=" + err.StreamPosition);
Console.WriteLine(" col=" + err.LinePosition);
}
它将显示以下内容(首先显示更正的文本,然后显示有关错误的详细信息):
<span style="color: #0000FF;"></span>
Error
code=EndTagNotRequired
reason=End tag </> is not required
text=<
line=1
pos=30
col=31
因此,您可以尝试修复此错误,因为您拥有所有必需的信息(包括行、列和流位置),但修复(不检测) HTML 中的错误的一般过程非常复杂。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)