Onlineatoz.net | ToolBox Standard Control XML

ToolBox Standard Control XML

XML Web Server Control

in ASP.NET environment,This article describes you how to work with Wizard Web Control.This XML control allows the user to work with this instead of working with Database.An Xml Web server control reads XML and writes it into a Web Forms page at the location of the control. If an XSL transformation (XSLT) is applied to the XML, the resulting transformed output will be rendered in the Web Forms page.

This XML web control enables two ways to reference external documents using property settings in the XML Web server control. first way can provide a path to the XML document in the control tag, second way can load the XML and XSLT documents as objects and then pass them to the control

Displaying an XML Document in a Page

To display XML information on a Web page,One way to provide these instructions is to create an ASP.NET Web page and then use code to parse the XML file and fill the data into the appropriate HTML tags. Although this is a somewhat time-consuming and inflexible way to do this, it is also a powerful way that gives you precise programmatic control over the XML file.

to use the XSL transformation language and create transformations, or XSL files, An XSL transformation contains the following information:

  • A template
  • -an HTML page with the appropriate tags — that specifies how the XML information should be displayed.
  • XSL processing instructions, which specify exactly how the information from the XML file fits into the template.

Once you have XSL transformations, you need to apply them to the XML file — that is, you process the XML file by transforming it according to one of the XSL files. The output is a new file with the XML information formatted according to the transformation file.

to use the Xml server control to display XML information using XSL transformations, you must have the following,

  • An XML file containing several fictional e-mail messages
  • Two XSL transformations. One displays only the date, sender, and subject of the e-mail messages. The other displays the entire e-mail message.
To Adding the XML File and XSL Transformations
  • In Solution Explorer, right-click the App_Data folder, and then click Add New Item.
  • Under Visual Studio installed templates, click XML file.
  • In the Name box, type TextXml.xml.
  • Click Add.
  • Copy the following XML data, and then paste it into the XML file
<?xml version="1.0" ?>
<MESSAGES>
<MESSAGE id="101">
<TO>Stanley</TO>
<FROM>Brinda</FROM>
<DATE>04 march 2006</DATE>
<SUBJECT>Subject1</SUBJECT>
<BODY>Type your details here</BODY>
</MESSAGE>
<MESSAGE id="109">
<TO>Stanley</TO>
<FROM>Sabir</FROM>
<DATE>04 march 2006</DATE>
<SUBJECT>Subject2</SUBJECT>
<BODY>Type your details
</BODY>
</MESSAGE>
<MESSAGE id="123">
<TO>Stanley</TO>
<FROM>Brinda</FROM>
<DATE>05 march 2006</DATE>
<SUBJECT>subject3</SUBJECT>
<BODY>enter your details</BODY>
</MESSAGE>
</MESSAGES>
  • Save the file and close it.

The next step is to add two XSL transformations to your project.

To add XSL transformations to your project
  • In Solution Explorer, right-click the App_Data folder, and then click Add New Item.
  • Under Visual Studio installed templates, click Text File.
  • and provide name as name.xslt
  • Click Add.
  • Copy the following example code and paste it into the file.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<BODY>
<TABLE cellspacing="3" cellpadding="8">
<TR bgcolor="#AAAAAA">
<TD class="heading"><B>Date</B></TD>
<TD class="heading"><B>From</B></TD>
<TD class="heading"><B>Subject</B></TD>
</TR>
<xsl:for-each select="MESSAGES/MESSAGE">
<TR bgcolor="#DDDDDD">
<TD width="25%" valign="top">
<xsl:value-of select="DATE"/>
</TD>
<TD width="20%" valign="top">
<xsl:value-of select="FROM"/>
</TD>
<TD width="55%" valign="top">
<B><xsl:value-of select="SUBJECT"/></B>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
  • Save the file and close it.
  • Repeat steps 1 through 4, but modify the file name name2.xslt.
  • Paste the following example code into the name2.xslt file.
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<HTML>
<BODY>
<FONT face="Verdana" size="2">
<TABLE cellspacing="10" cellpadding="4">
<xsl:for-each select="MESSAGES/MESSAGE">
<TR bgcolor="#CCCCCC">
<TD class="info">
Date: <B><xsl:value-of select="DATE"/></B><BR></BR>
To: <B><xsl:value-of select="TO"/></B><BR></BR>
From: <B><xsl:value-of select="FROM"/></B><BR></BR>
Subject: <B><xsl:value-of select="SUBJECT"/></B><BR></BR>
<BR></BR><xsl:value-of select="BODY"/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</FONT>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
  • Save the file and close it.
To add an Xml control to the Page
  • Open or switch to the Default.aspx page.
  • Switch to Design view.
  • From the Standard tab of the Toolbox, drag an Xml control onto the page.
  • Select the control, and in the Properties window, set the following properties.
  • DocumentSource ~/App_Data/TestXml.xml
  • TransformSource ~/App_Data/name.xslt
    • Move the insertion point in front of the Xml control and then press ENTER a few times to make space above the Xml control.
    • From the Standard tab of the Toolbox, drag a CheckBox control onto the form above the Xml control.
    • Set the following properties of the CheckBox control.
      • Text -Headers only
      • Checked -True
      • AutoPostBack -True
    • Double-click the CheckBox control.
    • copy the following code and paste it into the Checkbox event
  • Adding Controls to Change Transformations

    you will use a check box to allow users to switch between transformations. The Xml control currently applies a transformation that displays only the e-mail message headers.

    To add a check box to apply a different transformation
protected void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
if (CheckBox1.Checked)
{
Xml1.TransformSource = "~/App_Data/name.xslt";
}
else
{
Xml1.TransformSource = "~/App_Data/name2.xslt";
}
}
 
Related Links

Posted by: Admin
Posted on: 9/13/2011 at 2:00 PM
Categories: Asp.net
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed