How to tell if a html string will show as empty in the browser? (C# / regex?)

c#, html, regex, string

Solution

As answered by @Ignacio you should use something like the HTML Agility pack. Here's a sample bit of code that seems to work for your situation.

HtmlDocument docEmpty = new HtmlDocument();
docEmpty.LoadHtml("<p><br /></p>");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml("<p>I am not empty...<br /></p>");

bool shouldBeEmpty = string.IsNullOrEmpty(docEmpty.DocumentNode.InnerText);
bool shouldNotByEmpty = string.IsNullOrEmpty(doc.DocumentNode.InnerText);

Note: This sample uses the http://html-agility-pack.net/?z=codeplex parser.

Problem

I have a control that will return some html to me as a string. Before putting that on screen, I'd like to be able to tell if it'll just show as empty. For example the control might return `<p><br /></p>`, which when I test using C# for string.Emtpy obviously it's not - but nothing gets displayed on screen. Is there a regex function to test whether html will actually show any text on screen? Or using C# - is there any function to test the string containing html to see whether it actually contains anything other than tags? Cheers, I'm a little confused how to get around this without writing some custom parser, a road I don't want to have to go down!

Original source