#Create an HTML doc from C

14 בJanuary 2016

אין תגובות

Sometimes on server side we need to draw some reports to our domain folder,
If we already inside dotnet area  we can simplify the process with simple dotnet framework.
Using HtmlTextWriter can do the work and very easily , just assume that you know something or two about html, and the hierarchical structure of it,  than  you can continue from this struct to develop any webpage you need, start html tag , header body etc, of course can insert any css or js references,
Just do not forget to close any opened tags, so as a demo :


private static  string[] lines = new string[1000];

 

using (StreamWriter Swriter = new StreamWriter("c://newHtml.html"))

            {

                using (HtmlTextWriter writer = new HtmlTextWriter(Swriter))

                {

 

                    //<html Tag>

                    writer.RenderBeginTag(HtmlTextWriterTag.Html);

 

                    // <head Tag>

                    writer.RenderBeginTag(HtmlTextWriterTag.Head);

 

                    // </head>

                    writer.RenderEndTag();

 

                    // <body Tag>

                    writer.RenderBeginTag(HtmlTextWriterTag.Body);

 

                    // </div Tag>

                    writer.RenderBeginTag(HtmlTextWriterTag.Div);


                   // Loop over some strings data.

                    foreach (var line in lines)

                    {

                       // Begin Pre Tag 

                        writer.RenderBeginTag(HtmlTextWriterTag.Pre); 

                        writer.Write(line); 

                       // End Pre Tag

                        writer.RenderEndTag();                       

                    }

                    // </div>

                    writer.RenderEndTag();

 

                    // </body>

                    writer.RenderEndTag();

 

                    // </html>

                    writer.RenderEndTag();

 

                }

And that it !!
Again remember, unlike  HTML here every closing Tag is mandatory.

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *