'HTML Agility Pack not catching error in malformed HTML

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.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source