Dervin Thunk
Dervin Thunk

Reputation: 20119

Create HTML webpage programmatically in C#

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.

Upvotes: 8

Views: 20906

Answers (5)

Lorin
Lorin

Reputation: 1022

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

Upvotes: 6

Justin
Justin

Reputation: 86729

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.

Upvotes: 2

Sergey Shandar
Sergey Shandar

Reputation: 2387

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"]]
            ]
        ]
    ];

Upvotes: 2

Cheeso
Cheeso

Reputation: 192457

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

Upvotes: 7

Carlo
Carlo

Reputation: 25959

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.

Upvotes: 0

Related Questions