chillinOutMaxin
chillinOutMaxin

Reputation: 182

Confirming valid HTML tags in file using a stack C#

I need to read an HTML file and confirm that all of the HTML tags are properly closed using a Stack to do so. I am have trouble right now reading the file and finding each tag. I am not sure how I should proceed. Should I read each line of the file, use regex to find the tags, then add to the stack... or is there a better way to do this?

Upvotes: 0

Views: 311

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

Every hand-made solution you will attempt to use will become a nightmare. I really recommend you to use an external library that can handle HTML properly. With HTML Agility Pack this task becomes a joke:

// your string variable containing HTML
String html = ...

HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);

foreach (HtmlParseError error in document.ParseErrors)
{
    Console.WriteLine("ERROR: " + error.Code.ToString());
    Console.WriteLine(error.Reason);
    Console.WriteLine();
}

Upvotes: 2

Related Questions