How to handle ParseErrors in Html Agility Pack

c# html html-agility-pack

Question

I thus have a program that gathers the header and footer elements of an HTML document and adds them to a currently-existing web page. The material within the boxes shouldn't include html or body tags since both parts are already enclosed in div tags.

Having stated that, I'd want to search for open tags and programmatically shut them. I'm still learning how to use HTML Agility Pack, so I'm not sure how to get what I want. The code that I was able to deduce through Google searches is shown below.

    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 can I choose and close the tag that caused the error?

Thanks for your assistance in advance!

1
0
2/20/2014 3:05:39 PM

Accepted Answer

You may utilizedocument.OptionFixNestedTags = true; to automatically correct them.

You may look at the array list of errors in here.

To identify the issue, use the htmlParseError.Line method. Hope that's helpful.

1
2/20/2014 4:25:41 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow