How to handle ParseErrors in Html Agility Pack

c# html html-agility-pack

Question

So I have an application that collects the header and footer sections of an HTML document and inserts them into a preexisting web page. Both sections are already wrapped in div tags so the content within the boxes shouldn't have html/body tags.

That being said, I'd like to check for tags that aren't closed and programmatically close them. I'm very new to Html Agility Pack and I'm not sure how to accomplish what I want. Here is the code that I have been able to infer based upon Google searches.

    private bool RepairHtml(string htmlText)
    {
        var htmlDoc = new HtmlDocument();
        htmlDoc.OptionFixNestedTags = true;
        htmlDoc.LoadHtml(htmlText);
        var parseErrors = htmlDoc.ParseErrors;

        if (parseErrors != null)
        {
           foreach (var htmlParseError in parseErrors)
           {
              switch (htmlParseError.Code)
              {
                 case: HtmlParseErrorCode.TagNotClosed:
                    // Not sure what to do here
                    break;
              }
           }
        }
     }

How do I select the tag that generated an error and close it?

Thanks in advance for your help!

Accepted Answer

You can use document.OptionFixNestedTags = true; to fix them automatically.

You can check here for array list of errors.

You can use htmlParseError.Line to see what is causing error. Hope that helps




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why