Click here to Skip to main content
15,893,381 members
Articles / Web Development / ASP.NET

Generate dynamic ASP.NET pages by using XML and XSLT

Rate me:
Please Sign up or sign in to vote.
3.16/5 (55 votes)
15 Oct 20042 min read 522.6K   9K   136  
Generates dynamic forms by transforming XML into XSL file.
using System;
using System.Data;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace PubsWeb.XmlTransform
{
	/// <summary>
	/// Summary description for XmlXslTransforer.
	/// </summary>
	public class XmlXslTransforer
	{
		private readonly string XslFile = @"C:\inetpub\wwwroot\pubsweb\xmltransform\default.xslt";
		private readonly string XmlFile = @"C:\inetpub\wwwroot\pubsweb\xmltransform\cx.config";

		public System.Web.UI.WebControls.Panel ControlHolder;

		public XmlXslTransforer()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		public void TransformFields(string pageId)
		{
			/**
			 * Transform aspx page with fields from xml file
			 * Get transform result as a string
			 * Parse controls into a parent control holder
			 */

			XmlDocument xdoc = new XmlDocument();
			xdoc.Load(XmlFile);

			// load xslt to do transformation
			XslTransform xsl = new XslTransform();
			xsl.Load(XslFile);

			// load xslt arguments to load specific page from xml file
			// this can be used if you have multiple pages in your xml file and you loading them one at a time
			XsltArgumentList xslarg = new XsltArgumentList();
			xslarg.AddParam("pageid", "", pageId);

			// get transformed results
			StringWriter sw = new StringWriter();
			xsl.Transform(xdoc, xslarg, sw);
			string result = sw.ToString().Replace("xmlns:asp=\"remove\"","").Replace("&lt;","<").Replace("&gt;",">");

			// free up the memory of objects that are not used anymore
			sw.Close();

			// parse the controls and add it to the page
			Control ctrl = ControlHolder.Page.ParseControl(result);
			ControlHolder.Controls.Add(ctrl);	
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am Microsoft Certified in .NET
I have over seven-year experience with client-server application development (full project lifecycle) on Windows platforms.
I've focused on creating middle tier, web application development, GUI development, custom data management software, and database programming.

Comments and Discussions