Library tutorials & articles

Using ADO.NET, XML & XSL to generate HTML

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.

using System;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

namespace Menu
{
  class CreateMenu
  {
    [STAThread]
    static void Main(string[] args)
    {
      string source = @"Provider=Microsoft.Jet.OLEDB.4.0;Password="""";"
        + @"User ID=Admin;Data Source=C:\web\web.mdb";
      string select = "SELECT * FROM MenuItem";
      OleDbConnection conn = new OleDbConnection( source );
      conn.Open();
      OleDbDataAdapter adapter = new OleDbDataAdapter( select, conn );
      DataSet ds = new DataSet();
      adapter.Fill( ds );
      conn.Close();
      ds.Tables[0].TableName = "MenuItem";
      ds.DataSetName = "Menu";
      ds.WriteXml( @"C:\web\MenuText.xml" );
      XmlDataDocument doc = new XmlDataDocument( ds );
      XslTransform trans = new XslTransform();
      trans.Load( @"C:\web\Menu.xsl" );
      StreamWriter sw = new StreamWriter( @"C:\web\Menu.html" );
      trans.Transform( doc, null, sw );
      sw.Close();
    }
  }
}

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.

Comments

  1. 11 Dec 2005 at 12:31

    Hi,


    do you have any experiences about the performance of automate generated HTML by XSL & XML


    for example which one is faster:


    1. seperate Asp-Sites
    2. 1 Asp-Site that was generated by 1 XSL-Site (C# XslTransform)


    Thanks
    Schneeblitz

  2. 22 Sep 2005 at 09:42

    how would you be able to that in ASP.Net 2.0.....

  3. 14 Oct 2002 at 06:27
    Generating XML documents and associating them with style sheets is a great way of creating reports.
    The user can view database information, statistics, etc... using plain html text and print them in a neat
    format.
  4. 01 Jan 1999 at 00:00

    This thread is for discussions of Using ADO.NET, XML & XSL to generate HTML.

Leave a comment

Sign in or Join us (it's free).

Bill Burris
AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Nov 18

    15 Minutes of Fame

    Dresher, United States

    This is a yearly tradition. We select 10 of the favorite speakers from monthly meetings, code camps, and hands on labs. Each one does a 15 minute talk on their favorite .NET technology. This is our 10th anniversary so we plan a gala event with special prizes and refreshments.

Want to stay in touch with what's going on? Follow us on twitter!