Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create HTML webpage programmatically in C#

Tags:

html

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.

like image 418
Dervin Thunk Avatar asked Jun 01 '09 22:06

Dervin Thunk


4 Answers

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

like image 197
Cheeso Avatar answered Nov 10 '22 14:11

Cheeso


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

like image 40
Lorin Avatar answered Nov 10 '22 16:11

Lorin


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"]]
            ]
        ]
    ];
like image 40
Sergey Shandar Avatar answered Nov 10 '22 16:11

Sergey Shandar


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.

like image 2
Justin Avatar answered Nov 10 '22 14:11

Justin