views:

48

answers:

1

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? Thanks!!

EDIT:

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>
    <% 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? Thanks again everyone!

+1  A: 

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.

Robert Rossney
Thanks for such a detailed response and for cuing me in on XSLT. I haven't used those kinds of templates before but I think it's something I'd be able to take advantage of in a number of situations!! How hard is it to go from XML to XAML using XSLT (is it much harder than xml to html)? It wouldn't help for my current project, but it seems like a great way to standardize how information is displayed across both web apps and desktop apps?
evan
It's not at all hard to convert XML to XAML. In fact once you understand how `XamlReader` works, there's a lot of interesting and cool stuff that you can do with the XML -> XSLT -> XAML -> CLR objects pipeline. If there's a hard part about what you suggest, it's figuring out what the material differences between HTML/CSS and the `FlowDocument`'s object model are. I haven't looked into it in any detail, but I'd bet folding money that there are a ton.
Robert Rossney