tags:

views:

2493

answers:

3

Hello. I was wondering: is there a way to create HTML files programmatically in C# as you can do with XML? Mine is a console application, so maybe some of the options are not available. Basically, I would like to do something smarter than just building a big string.

Possible scenario:

Instead of writing:

     string html="<html><head>Blah</head><body>{0}</html>", myotherstring

I would like to work as in XML

     XmlTextWriter w = new XmlTextWriter(xml_file_path + xml_file_name,
                                        System.Text.Encoding.UTF8);

     w.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");

     // construct xml
     XmlElement root = xmlDoc.CreateElement("element");

     ...

     xmlDoc.Save(w);
     w.Close();

Apologies for the naive question.

A: 

What I did a few months back, I had an asp.net file (aspx) saved as a template in a text file, whenever the user needed a new page, I would just copy that template into the user specified folder, change the extension .txt to .aspx, and programmatically add a few options depending on the user's needs. It was a simple page though. Of course, the more complex you go, the more complex the code will be.

Carlo
+3  A: 

You could use NVelocity. It is a .Net port of the Java Velocity templating system. The API will not be similar to XmlWriter. Instead, you'll write a text file in a simple scripting language, put your objects into a 'context' and then merge the template and the context to generate the HTML file.

  1. NVelocity
  2. Velocity Syntax Guide
Lorin
+5  A: 

Don't forget: You can generate XHTML just as easily as plain XML using the XmlTextWriter approach.

Cheeso
+1 for this. But for my current problem, I found NVelocity met my purposes wonderfully.
Dervin Thunk