HTML Agility Pack 无法捕获格式错误的 HTML 中的错误
我正在使用 HTML Agility Pack 来验证 HTML 是否有效,然后再发送格式正确或格式错误的电子邮件。
我当前正在测试的代码是
void Main()
{
var htmlDoc = new HtmlDocument();
htmlDoc.OptionWriteEmptyNodes = true;
var htmlMessage = GetGtml();
htmlDoc.LoadHtml(htmlMessage);
var errors = htmlDoc.ParseErrors.Count();
Console.WriteLine(errors);
}
private string GetGtml()
{
var sb = new StringBuilder();
sb.AppendLine("<h1>Email Test</h1>");
sb.AppendLine("<h5>Test</h5>");
sb.AppendLine("<p>This email example has 5 elements to it.</p>");
sb.AppendLine("<ul>");
sb.AppendLine(" <li>h1</li>");
sb.AppendLine(" <li>h5</li>");
sb.AppendLine(" <li>p</li>");
sb.AppendLine(" <li>ul \\ li</li>");
sb.AppendLine(" <li>img</li>");
sb.AppendLine("</ul>");
sb.AppendLine("img src=\"\" alt=\"\"/>");
return sb.ToString();
}
我遇到的问题是行 img src="" alt=""/>
没有打开 <
,但是html 功能包仍将其显示为有效的 HTML。
我期待的是 HTML 解析错误,如下
Code: TagNotOpened
Reason : Start tag xxx was not found
问题:如何验证 \ 确保 img 标签正确打开,并在未正确打开时抛出适当的错误 \ 异常。
I am working with HTML Agility Pack to validate HTML is valid before sending out a well formed or a malformed email.
The code I am testing with currently is
void Main()
{
var htmlDoc = new HtmlDocument();
htmlDoc.OptionWriteEmptyNodes = true;
var htmlMessage = GetGtml();
htmlDoc.LoadHtml(htmlMessage);
var errors = htmlDoc.ParseErrors.Count();
Console.WriteLine(errors);
}
private string GetGtml()
{
var sb = new StringBuilder();
sb.AppendLine("<h1>Email Test</h1>");
sb.AppendLine("<h5>Test</h5>");
sb.AppendLine("<p>This email example has 5 elements to it.</p>");
sb.AppendLine("<ul>");
sb.AppendLine(" <li>h1</li>");
sb.AppendLine(" <li>h5</li>");
sb.AppendLine(" <li>p</li>");
sb.AppendLine(" <li>ul \\ li</li>");
sb.AppendLine(" <li>img</li>");
sb.AppendLine("</ul>");
sb.AppendLine("img src=\"\" alt=\"\"/>");
return sb.ToString();
}
The issue that I am havingis that the line img src="" alt=""/>
has no opening <
, however html ability pack is showing this still as valid HTML.
What I was expecting is a HTML Parse Error something like this
Code: TagNotOpened
Reason : Start tag xxx was not found
Question: How do I validate \ ensure the img tag is correctly opened and throws an appropriate error \ exception when its not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论