Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » General » Downloads
 
Add your own
alternative version

Generate dynamic ASP.NET pages by using XML and XSLT

, 15 Oct 2004
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

Share

About the Author

rafael_p
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.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 15 Oct 2004
Article Copyright 2004 by rafael_p
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid