views:

19

answers:

1

I have a requirement to generate an HTML snapshot of an object - basically on my app you can click on the Clipboard button and generate either Text, BB Bode or HTML and it goes through the object and builds a string of text which is copied to the Clipboard. You can then paste this snippet on forums, blogs, websites (ie. a way of sharing).

What is the most efficient way of writing HTML output? In ASP.NET I would use HtmlTextWriter but I can't use the System.Web assembly in Silverlight. I could write the tags manually but I was hoping there was a better way.

Note: This is nothing to do with the current HTML page or displaying HTML in Silverlight or Silverlight in HTML. The requirements are valid ;)

+1  A: 

The closest thing to HTmlTextWriter thats actually available in Silverlight is XmlWriter.

StringBuilder sb = new StringBuilder();
XmlWriter writer = new XmlWriter(sb);

// use writer to create html content.

string html = sb.ToString();

Not as slick as using HtmlTextWriter but better than using StringBuilder directly. Just watch out for those elements that need a closing tag such as <div></div>.

AnthonyWJones
Thanks Anthony, I spent about an hour Googling that but guess I was looking for the wrong thing! That set me one the right track..
Rodney