What's the easiest way to create an HTML file?

I'm working on a console app that should spit out an html document containing a table and possibly some javascript.

I was thinking about writing the html by hand:

streamWriter.WriteLine("<html>");
streamWriter.WriteLine("<body>");
streamWriter.WriteLine(GetHtmlTable());
streamWriter.WriteLine("</body>");
streamWriter.WriteLine("</html>");

      

... but was wondering if there is a more elegant way to do this. Something like this:

Page page = new Page();
GridView gridView = new GridView();
gridView.DataSource = GetDataTable();
gridView.DataBind();

page.Controls.Add(gridView);
page.RenderControl(htmlWriter);
htmlWriter.Flush();

      

Assuming I'm on the right track, what's the correct way to build the rest of the html document (ie: html, head, title, body elements) using a class System.Web.UI.Page

? Do I need to use literal controls?

+3


source to share


2 answers


I am doing a lot of automated HTML page generation. I like to create an HTML page template with custom tags where dynamic controls, data or literals need to be inserted. Then I read the template file in line and replace the native tag with the generated HTML as you do above and write the HTML file back from the line. It saves time building all the tedious HTML support for design template, css and JS support.

Sample template file

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <CUSTOMHEAD />
    </head>
    <body>
        <CUSTOMDATAGRID />
    </body>
</html>

      



Create HTML file from template loaded in string Example

    private void GenerateHTML(string TemplateFile, string OutputFileName)
    {

        string strTemplate = TemplateFile;
        string strHTMLPage = "";
        string strCurrentTag = "";

        int intStartIndex = 0;
        int intEndIndex = 0;


        while (strTemplate.IndexOf("<CUSTOM", intEndIndex) > -1)
        {
            intStartIndex = strTemplate.IndexOf("<CUSTOM", intEndIndex);

            strHTMLPage += strTemplate.Substring(intEndIndex, 
                                                 intStartIndex - intEndIndex);

            strCurrentTag = strTemplate.Substring(intStartIndex,
                 strTemplate.IndexOf("/>", intStartIndex) + 6 - intStartIndex);
            strCurrentTag = strCurrentTag.ToUpper();

            switch (strCurrentTag)
            {
                case "<CUSTOMHEAD />":
                    strHTMLPage += GenerateHeadJavascript();
                    break;

                case "<CUSTOMDATAGRID />":
                    StringWriter sw = new StringWriter();
                    GridView.RenderControl(new HtmlTextWriter(sw));
                    strHTMLPage += sw.ToString();
                    sw.Close();
                    break;

                case "<CUSTOMANYOTHERTAGSYOUMAKE />":
                    //strHTMLPage += YourControlsRenderedAsString();
                    break;

            }
            intEndIndex = strTemplate.IndexOf("/>", intStartIndex) + 2;
        }

        strHTMLPage += strTemplate.Substring(intEndIndex);

        try
        {
            StreamWriter swHTMLPage = new System.IO.StreamWriter(
                                                OutputFileName, false, Encoding.UTF8);
            swHTMLPage.Write(strHTMLPage);
            swHTMLPage.Close();
        }
        catch (Exception ex)
        {
            // AppendLog("Write File Failed: " + OutputFileName + " - " + ex.Message);
        }
    }

      

+2


source


It would be nice to use a templating system to debug your presentation and business logic.

Take a look at the Razor Generator, which lets you use CSHTML templates in non-ASP.NET applications.



http://razorgenerator.codeplex.com/

+3


source







All Articles