Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking a HTML string for unopened tags

I have a string as a HTML source and I want to check whether the HTML source which is string contains a tag which is not opened.

For example the string below contains </u> after WAVEFORM which has no opening <u>.

WAVEFORM</u> YES, <u>NEGATIVE AUSCULTATION OF EPIGASTRUM</u> YES,

I just want to check for these types of unopened tag and then I have to append the open tag to the start of the string?

like image 469
subash Avatar asked Jul 02 '10 09:07

subash


People also ask

How do I check if a string is valid in HTML?

You could check if the string contains at least one < and at least one > and call it HTML, or you could check that it is strictly valid with correct HTML syntax, or anything from between. For the simplest of cases a HTML parser is not necessary.

How do you check tags in HTML?

Right-click while on your webpage in Google Chrome. Click 'Inspect' You'll see the HTML code in a box at the side or bottom of your page. Use Ctrl + F to find particular tags or elements.

How do I find missing tags in HTML?

You can use the Auto-Format feature (Ctrl+K+D) of Microsoft Visual Studio - it reformats your code so that you can easily see whether there are missing tags. I love this feature, it often comes in handy. Save this answer. Show activity on this post.

How are all the HTML tags enclosed?

Tags are always enclosed in angle brackets: < >. Tags are comprised of elements and attributes. An element is an object on a page (such as a heading, paragraph, or image), and attributes are qualities that describe that element (such as width and height). Tags usually travel in pairs.


1 Answers

For this specific case you can use HTML Agility Pack to assert if the HTML is well formed or if you have tags not opened.

var htmlDoc = new HtmlDocument();

htmlDoc.LoadHtml(
    "WAVEFORM</u> YES, <u>NEGATIVE AUSCULTATION OF EPIGASTRUM</u> YES,");

foreach (var error in htmlDoc.ParseErrors)
{
    // Prints: TagNotOpened
    Console.WriteLine(error.Code);
    // Prints: Start tag <u> was not found
    Console.WriteLine(error.Reason); 
}
like image 165
João Angelo Avatar answered Sep 30 '22 07:09

João Angelo