8

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.

1
  • what does smarter mean? Could you describe the scenario? Commented Jun 1, 2009 at 22:59

5 Answers 5

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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.

NVelocity

2 Comments

Alas, the links are broken.
@HaraldCoppoolse - No worries! I fixed 'em last month.
2

You could use some third party open-source libraries to generated strong typed verified (X)HTML, such as CityLizard Framework or Sharp DOM.

For example

html
    [head
        [title["Title of the page"]]
        [meta_(
            content: "text/html;charset=UTF-8",
            http_equiv: "Content-Type")
        ]
        [link_(href: "css/style.css", rel: "stylesheet", type: "text/css")]
        [script_(type: "text/javascript", src: "/JavaScript/jquery-1.4.2.min.js")]
    ]
    [body
        [div
            [h1["Test Form to Test"]]
            [form_(action: "post", id: "Form1")
                [div
                    [label["Parameter"]]
                    [input_(type: "text", value: "Enter value")]
                    [input_(type: "submit", value: "Submit!")]
                ]
            ]
            [div
                [p["Textual description of the footer"]]
                [a_(href: "http://google.com/")
                    [span["You can find us here"]]
                ]
                [div["Another nested container"]]
            ]
        ]
    ];

Comments

2

I realise that this question is old, however the recent release of the ASP.Net MVC 3 Razor view engine now gives you the option to use this same Razor view engine to generate HTML for any purpose.

See Hosting Razor outside of ASP.Net for a guide on how to do this.

1 Comment

I've looked at the zip file and it doesn't appear to do much. It certainly doesn't generate any html.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.