Silverlight tip – managing HTML

Posted by Corey on September 29, 2009 at 5:45 pm.

image

Problem: Silverlight 2 & Silverlight 3 do not support HTML, how can I remove the tags?

Solution: Although there are many solutions to this problem, the quickest and most frequently solution I use is a regular expression to remove HTML tags.  The most straightforward example I’ve found is from John Papa’s book Data Driven Services with Silverlight 2

// Remove HTML tags and empty newlines and spaces and leading spaces
string formattedValue = REgex.Replace(value as string, "<.*?>", "");
formattedValue = Regex.Replace(formattedValue, @"\n+\s+", "\n\n");
formattedValue = formattedValue.TrimStart(’ ‘);
fromattedValue = HttpUtility.HtmlDecode(formattedValue);
if(length > 0 && formattedValue.Length >= length)
  formattedvalue = formattedValue.Substring(0, length - 1);
return formattedValue;

2 Responses to “Silverlight tip – managing HTML”

Leave a Reply