Generating HTML Programmatically in C#, Targeting Printed Reports

Posted by John Passaniti on Stack Overflow See other posts from Stack Overflow or by John Passaniti
Published on 2010-06-05T07:16:40Z Indexed on 2010/06/05 7:22 UTC
Read the original article Hit count: 135

Filed under:

I've taken over a C# (2.0) code base that has the ability to print information. The code to do this is insanely tedious. Elements are drawn onto each page, with magic constants representing positioning. I imagine the programmer sitting with a ruler, designing each page by measuring and typing in the positions. And yes, one could certainly come up with some nice abstractions to make this approach rational. But I am looking at a different method.

The idea is that I'll replace the current code that prints with code that generates static HTML pages, saves them to a file, and then launches the web browser on that file. The most obvious benefit is that I don't have to deal with formatting-- I can let the web browser do that for me with tags and CSS.

So what I am looking for is a very lightweight set of classes that I can use to help generate HTML. I don't need anything as heavyweight as HTMLTextWriter. What I'm looking for is something to avoid fragments like this:

String.Format("<tr><td>{0}</td><td>{1}</td></tr>", foo, bar);

And instead take have this kind of feel:

...
table().
    tr().
        td(foo).
        td(bar)

Or something like that. I've seen lightweight classes like that for other languages but can't find the equivalent (or better) for C#. I can certainly write it myself, but I'm a firm believer in not reinventing wheels.

Know anything like this? Know anything better than this?

© Stack Overflow or respective owner

Related posts about c#