I have a WinForms application, running on .net 3.5. This Application generates HTML on the fly, which includes the complete document, and also an inline-CSS-Stylesheet (inside the head element).
I am using the WebBrowser control and setting browser.DocumentText to my generated HTML, but that does not seem to properly apply styles on the body element (I've set background-color to #000000 in the CSS, but the background is still white).
I wonder if a) there are some alternatives to render relatively simple HTML in C# (i.e. a completely managed HTML renderer) or b) what would be the best way to render HTML using the WebBrowser control, including correct handling of inline-css and without using a temporary file on the hard drive.
Edit: The CSS-not-applying issue was a separate problem. I've put my actual CSS in a CDATA block, which seems to cause it not to apply correctly. That is now fixed, but the question itself still stands.
For managed HTML renderer see HtmlRenderer on GitHub.
Stayed with the built-in WebBrowser. The HtmlRenderer from the other answer is great, but renders an image.
Your could look into using a view engine. Most view engines can work in a stand-alone api manner
for example NHaml and nvelocity both can be used stand alone
http://code.google.com/p/nhaml/
http://www.castleproject.org/others/nvelocity/index.html
And i assume Spark can be as well
I have developed what you were asking for: a "completely managed HTML renderer", which will "render relatively simple HTML in C#". It supports a subset of CSS, which you can "specify in a inline CSS stylesheet (inside the head element)".
For details, see the ModelText HTML control.
来源:https://stackoverflow.com/questions/1218325/best-way-to-render-html-in-winforms-application