Introduction
If your website has several pages with a similar layout it can become a pain to update using an HTML editor. What is needed is a way to automate the page generation from database tables, XML, & XSL files. By separating content and page layout for you website it is much easier to update repetitive content or easily change the style of every page. Simple content can be maintained in a database. Other content can be maintained in XML files. More complex content can be generated using your favorite HTML editor without worrying about the extras that go on the top, bottom and sides of the page. Using a program or script all the pieces can be put together to build a complete website. A batch process can be used to generate the pages to be uploaded to a server where you don't have access to server side scripting or programming. By keeping your content and style sheets modular you will be more prepare for the day when you can move your site to an ASP.NET server. Modular content will be needed when you start thinking about supporting multiple browser types, such as mobile devices and voice.
The Database
To give an idea about the tools which can be used, lets automate the creation of a side-bar menu. In this example the menu will be saved as a complete HTML file. In a real application the resulting HTML or XHTML would be inserted into the page which is being built from multiple sources. The advantage of using XHTML would be the ability to use all the XML tools in .NET for building your page.
Here is the database table which has the content for the menu.
Here is the code for reading the database table and converting it to HTML.
This code opens a connection to the database and reads the table into a DataSet. The TableName and DateSetName are given names which will result in meaningful tags in the XML. The DataSet is saved as XML using ds.WriteXml in case we want to take a look at it. The DataSet is converted to an XmlDataDocument so that we can apply the transform to it. The XSL stylesheet is loaded into the XslTransform object. A StreamWriter is used by the XslTransform object to write the resulting HTML to a file.
The XML
Here is the XML generated by ds.WriteXml:
Here is the XSL used to transform the XML into HTML:
The HTML
Here is the resulting HTML:
The ADO.NET and XML support in the .NET framework is a powerful and easy to use combination. These tools can be used for batch generation of web pages, or used live in ASP.NET.
Leave your greetings.