检查未开封的标签一个HTML字符串字符串、标签、HTML

2023-09-04 02:43:33 作者:耗尽温柔

我有一个字符串作为HTML源,我想检查是否HTML源代码是字符串中包含这是不打开的标签。

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.

例如字符串下面包含< / U> 波形没有开通后< U>

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?

推荐答案

有关,您可以使用 HTML敏捷性包这种特定情况下断言如果HTML结构良好,或者如果你有标签不开了。

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); 
}