C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Generating HTML Page from an XML File in ASP.NET


Posted Date: 06 Jun 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms
Author: sriluMember Level: Diamond    
Rating: Points: 10



This code sample generates a HTML page,with controls in it from an XML file.The XML file contains the collection of controls which u require in your page.We use an XSL to define the XML file's structure and to transform it into the required format.

The following is an example XML file named "controls.xml"


<?xml version="1.0" encoding="UTF-8" ?>
<controls>
<control type="text" name="Title" required="yes"/>
<control type="text" name="Industry"/>
<control type="label" name="Education"/>
<control type="Button" name="Test"/>
</control>
</controls>


The corresponding "controls.XSL" looks like follows(This XSL helps generate the aspx page)


<?xml version="1.0" encoding="UTF-8" ?>
<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:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="5">
<xsl:for-each select="//question">
<tr>
<td valign="top">
<xsl:value-of select="@name" />
</td>
<td>
<xsl:if test="@type='text'">
<input id="{@name}" type="{@type}" runat="server"/>
</xsl:if>
<xsl:if test="@required = 'yes'">
<asp:RequiredFieldValidator id="reqfield" ErrorMessage="Required Field" runat="server" ControlToValidate="{@name}" />
</xsl:if>
<xsl:if test="@type='label'">
<input id="{@name}" type="{@type}" runat="server"/>
</xsl:if>
<xsl:if test="@type='Button'">
<input id="{@name}" type="{@type}" runat="server"/>
</xsl:if>
</td>
</tr>
</xsl:for-each>
</table>
<asp:button id="submit" runat="server" Text="Submit" />
</form>
</body>
</html>
</xsl:template>
</xsl:stylesheet>


Now in your code behind, we need to write the code to transform the XML File based on XSL File

string XmlFile = Server.MapPath("controls.xml");
string XslFile = Server.MapPath("controls.xsl");
XmlDocument xdoc = new XmlDocument();
xdoc.Load(XmlFile);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(XslFile);
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("page.html"),Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 4;
transform.Transform(xdoc, null, writer);


This adds a new HTML page, "page.html", to your project folder with the controls defined in your XML file.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Generate html pages from xml using xsl  .  Generate html pages from xml  .  Generate html from xml  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to Generate Webpage from an XML File
Previous Resource: Play Flash Files in ASP.NET
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use