Here is a way to validate XHTML using methods similar to StripTagsCharArray. We count the number of tag chars and make sure the counts match.
using System;
class Program
{
static void Main()
{
// Test the IsValid method.
Console.WriteLine(HtmlUtil.IsValid(
"<html><head></head></html>"));
Console.WriteLine(HtmlUtil.IsValid(
"<html<head<head<html"));
Console.WriteLine(HtmlUtil.IsValid(
"<a href=y>x</a>"));
Console.WriteLine(HtmlUtil.IsValid(
"<<>>"));
Console.WriteLine(HtmlUtil.IsValid(
""));
}
}
static class HtmlUtil
{
enum TagType
{
SmallerThan,
// "<"
GreaterThan
// ">"
}
public static bool IsValid(string html)
{
TagType expected = TagType.SmallerThan;
// Must start with "<"
for (int i = 0; i < html.Length; i++)
// Loop
{
bool smallerThan = html[i] == '<';
bool greaterThan = html[i] == '>';
if (!smallerThan && !greaterThan)
// Common case
{
continue;
}
if (smallerThan && expected == TagType.SmallerThan)
// If < and expected continue
{
expected = TagType.GreaterThan;
continue;
}
if (greaterThan && expected == TagType.GreaterThan)
// If > and expected continue
{
expected = TagType.SmallerThan;
continue;
}
return false;
// Disallow
}
return expected == TagType.SmallerThan;
// Must expect "<"
}
}
True
False
True
False
True