The XML Web Control

only for RuBoard

As you have just seen, performing an XSL transformation does not require a large amount of code after you have the XML document and XSLT template ready. With ASP.NET, however, Microsoft has supplied a control that makes XSL transformations even easier. In many cases, you can accomplish the same transformation with as little as a single tag, using the <asp:Xml> control:

 <asp:Xml id="PitcherDisplay" runat="server"      DocumentSource="pitchers.xml"      TransformSource="pitcherstyle.xslt" /> 

If both your XML and XSLT are located in local files and you don't need to pass any arguments to your XSLT, a single <asp:Xml> tag is all you need to apply the transformation and output it to the browser. However, what if your needs are more complicated? Perhaps you need to pull the XML data from a remote server or a database ”or perhaps you simply need to pass an argument or two to your XSLT stylesheet. You're in luck because the <asp:Xml> control was designed to accommodate those needs. The control has additional properties that accept an XmlDocument object (which can be loaded from anywhere ) for the XML data, an XslTransform object for the XSL transformation, and an XsltArgumentList object for any arguments that your XSLT template might require. The properties are called Document , Transform , and TransformArgumentList , respectively. These properties expect live objects so they cannot be set declaratively in the tag, but must be set in the code for the page.

You might have noticed that the single tag listed previously duplicates the last example almost perfectly , with the exception that we haven't told the stylesheet what sort -order we'd like. Let's complete the duplication of the last example so that you can compare the amount of effort required for each method. First, create a new Web Form in Visual Studio, and add an XML control from the Toolbox to the page. Call it PitchersDisplay and add values for the DocumentSource and TransformSource properties, as you just saw in the preceding sample tag.

Now switch to the code view for the page. You need to add some using (or Imports , if you're working with Visual Basic) statements to the top of the page ”one for System.Xml and one for System.Xml.Xsl . In the Page_Load event handler, add the code shown in Listing 6.16.

Listing 6.16 Transforming Using the asp:xml Control
 using System;  using System.Collections;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Web;  using System.Web.SessionState;  using System.Web.UI;  using System.Web.UI.WebControls;  using System.Web.UI.HtmlControls;  using System.Xml;  using System.Xml.XPath;  using System.Xml.Xsl;  namespace XSLT  {      /// <summary>       /// Summary description for pitch.       /// </summary>       public class pitch : System.Web.UI.Page       {           protected System.Web.UI.WebControls.Xml PitchersDisplay;            private void Page_Load(object sender, System.EventArgs e)            {                XmlTextReader reader = new  XmlTextReader(Server.MapPath("pitchers.xml"));                 XPathDocument doc = new XPathDocument(reader);                 reader.Close();                 reader = new  XmlTextReader(Server.MapPath("pitcherstyle.xslt"));                 XslTransform transform = new XslTransform();                 transform.Load(reader);                 XsltArgumentList args = new XsltArgumentList();                 args.AddParam("sortBy", string.Empty, "TEAM");                 PitchersDisplay.TransformArgumentList = args;            }            #region Web Form Designer generated code            override protected void OnInit(EventArgs e)            {                //                 // CODEGEN: This call is required by the ASP.NET Web Form                 // Designer.                 //                 InitializeComponent();                 base.OnInit(e);            }            /// <summary>            /// Required method for Designer support - do not modify            /// the contents of this method with the code editor.            /// </summary>            private void InitializeComponent()            {           this.Load += new System.EventHandler(this.Page_Load);            }            #endregion       }  } 

And there you have it. You've done an XSLT transformation, complete with arguments, using a single tag and three lines of code. Using the Document and Transform properties of this control is equally straightforward; simply create your XmlDocument or XslTransform objects as you would if you were performing the transformation yourself, and pass them to the appropriate property of the XML Web Control.

only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net