How To Create ASP.NET Control From XML using XSLT

We are sometimes asked to view XML data in a style/design format. When we browse an XML file directly, the XML nodes with data are shown. As a result, XSLT aids in the presentation of XML data in a design/specific format.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMLControl.aspx.cs"  
Inherits="XMLControl" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<head runat="server">  
<title>Untitled Page</title>  
<form id="form1" runat="server">  
      <asp:PlaceHolder ID="survey"  
      <asp:Label ID="ThankYouLabel" runat="server"  
Visible="true" Text="Thank You"></asp:Label>  
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Xml;  
using System.Xml.Xsl;  
using System.Xml.XPath;  
using System.IO;  

public partial class XMLControl : System.Web.UI.Page  
protected void Page_Load(object sender, EventArgs e)  

protected override void OnInit(EventArgs e)  
// called from Page's Init event handler   
private void CreateControl()  
 // Load the data source   
 XPathDocument surveyDoc = new XPathDocument(Server.MapPath("Control.xml"));  

 // Load the xslt to do the transformations   
 XslTransform transform = new XslTransform();  

 // Get the transformed result   
 StringWriter sw = new StringWriter();  
 transform.Transform(surveyDoc, null, sw);  
 string result = sw.ToString();  

 // remove the namespace attribute   
 result = result.Replace("xmlns:asp=\"remove\"", "");  

 // parse the control(s) and add it to the page   
 Control ctrl = Page.ParseControl(result);  



<xsl:stylesheet version="1.0"  
xmlns:xsl="" xmlns:asp="remove">  
  <xsl:output method="xml" indent="yes" encoding="utf-8"  
  <xsl:template match="/">  
          <xsl:for-each select="//question">  
                  <td valign="top">  
                      <xsl:value-of select="@name" />  
                      <xsl:if test="@required = 'yes'">  
                          <asp:RequiredFieldValidator ErrorMessage="Required  
Field" runat="server" ControlToValidate="{@name}" />  
                      <xsl:if test="@type='text'">  
                          <asp:TextBox id="{@name}" runat="server" />  
                      <xsl:if test="@type='radio'">  
                          <asp:RadioButtonList id="{@name}" runat="server">  
                              <xsl:for-each select="choice">  
                                  <asp:ListItem Value="{@value}" Text="{@value}">  
                                      <xsl:value-of select="current()"/>  
                      <xsl:if test="@type='select'">  
                          <asp:DropDownList id="{@name}" runat="server">  
                              <xsl:for-each select="choice">  
                                  <asp:ListItem Value="{@value}" Text="{@value}">  
                                      <xsl:value-of select="current()"/>  
      <asp:button id="submit" runat="server" Text="Submit" />  


<survey name="Example Survey">  
   <question type="text" name="Title" required="yes"/>  
   <question type="text" name="Industry"/>  
   <question type="radio" name="Education">  
       <choice>High school</choice>  
       <choice>Some college</choice>  
   <question type="select" name="Country">  
Next Post Previous Post
No Comment
Add Comment
comment url