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"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
<title>Untitled Page</title>  
</head>  
<body>  
<form id="form1" runat="server">  
  <div>  
      <asp:PlaceHolder ID="survey"  
runat="server"></asp:PlaceHolder>  
      <asp:Label ID="ThankYouLabel" runat="server"  
Visible="true" Text="Thank You"></asp:Label>  
  </div>  
</form>  
</body>  
</html>  
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)  
{  
 base.OnInit(e);  
 CreateControl();  
}  
// 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();  
 transform.Load(Server.MapPath("Control.xsl"));  

 // 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);  
 survey.Controls.Add(ctrl);  
}  

}  

Control.XSL

<xsl:stylesheet version="1.0"  
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove">  
  <xsl:output method="xml" indent="yes" encoding="utf-8"  
omit-xml-declaration="yes"></xsl:output>  
  <xsl:template match="/">  
      <table>  
          <xsl:for-each select="//question">  
              <tr>  
                  <td valign="top">  
                      <xsl:value-of select="@name" />  
                      <xsl:if test="@required = 'yes'">  
                          <asp:RequiredFieldValidator ErrorMessage="Required  
Field" runat="server" ControlToValidate="{@name}" />  
                      </xsl:if>  
                  </td>  
                  <td>  
                      <xsl:if test="@type='text'">  
                          <asp:TextBox id="{@name}" runat="server" />  
                      </xsl:if>  
                      <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()"/>  
                                  </asp:ListItem>  
                              </xsl:for-each>  
                          </asp:RadioButtonList>  
                      </xsl:if>  
                      <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:ListItem>  
                              </xsl:for-each>  
                          </asp:DropDownList>  
                      </xsl:if>  
                  </td>  
              </tr>  
          </xsl:for-each>  
      </table>  
      <asp:button id="submit" runat="server" Text="Submit" />  
  </xsl:template>  
</xsl:stylesheet>  

>Control.XML  

  
```xml
  
<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>  
       <choice>College</choice>  
   </question>  
   <question type="select" name="Country">  
       <choice>India</choice>  
       <choice>UK</choice>  
   </question>  
</survey>

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru