Madviola
Madviola

Reputation: 59

How to handle ParseErrors in Html Agility Pack

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!

Upvotes: 0

Views: 3434

Answers (1)

Kuzgun
Kuzgun

Reputation: 4737

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

Upvotes: 1

Related Questions