How to output HTML in Silverlight 4 to a string (like HtmlTextWriter)
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 Silverl开发者_如何转开发ight or Silverlight in HTML. The requirements are valid ;)
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>
.
精彩评论