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.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|