WPF application which outputs html
I'm creating a WPF (in c#) application that will create html files based on it's output. Is there a way to use the concept of an html 'view' (from the asp.net mvc) in a desktop application without reinventing the wheel? Basically I just want to save a list of outputted items in html which is so simple in a web application (and even using binding in a wpf desktop application to show the output in window) but seems clunky for HTML output in a desktop application I feel like I'd be reinventing the wheel to include flexible html templates with my program for data output.
Surely there is a quick and elegant way to do this?
As an example say the output of the program were a list of names (Bill, Bob, Jill, Frank), I'd like to save the following HTML file to the disk:
<html>
<body>
<ul>
<li>Bill</li>
<li>Bob</li>
<li>Jill</li>
<li>Frank</li>
</ul>
</body>
</html>
if the user defined a an html template such as:
<html>
<body>
<ul>
<li><!-- user name --></li>
</ul>
</body>
</html>
which could be done in ASP.NET MVC (or in a very similar way in PHP, ruby, etc) if the user defined something similar to the following:
<html>
<body>
<ul>
&开发者_StackOverflow社区lt;% foreach (string name in NameList) { %>
<li>
<%: name %>
</li>
<% } %>
</ul>
</body>
</html>
and I'd like to know if there is an easy way to create html files using that method in a desktop application?
Store your data in a form that is readily serialized to XML - either in a class that implements XML serialization, or an ADO.NET DataSet
. Write XSLT templates for transforming the XML into XHTML. Implement the transformations using the XslCompiledTemplate
class.
Granted, that approach will require you to learn some things that you probably don't know very much about. XSLT's not trivial, and there are subtleties in XML, XSLT, and HTML that will trip you up from time to time. But once you learn it, it's an incredibly powerful combination of technologies. I haven't produced an HTML document by hand in a decade; my starting point for any HTML document that I have to produce is an XML document that contains its content and an XSLT transform for rendering the content.
From the WPF side, the only issue that you'll need to deal with is in displaying rendered HTML in your application. I've done this two ways. One is by wrapping the WinForms WebBrowser
control in a UserControl
that exposes an Html
dependency property, so that I can bind my view models' Html
property to it. (The view models all share a helper class for getting their XML and transforming it to HTML.)
Another, harder way to get the output into a WPF UI is to bypass HTML completely and write an XSLT transform that renders the data as a FlowDocument
in XAML. It's not actually harder per se, it just introduces a bunch of new issues that you have to think about - also, it may be totally inappropriate for your application, if HTML production is truly what it's all about. But once you know how to transform XML data into HTML, transforming it into XAML is child's play.
精彩评论