This documentation is archived and is not being maintained.

Transforming XML Data in the XML Web Server Control

Visual Studio .NET 2003

An XSL transformations (XSLT) style sheet (.xslt or .xsl file) is used to transform the content of a source XML document into a presentation that is tailored specifically to a particular user, media, or client. For an overview, see the XSLT Developer's Guide. There are two ways to transform XML data in the XML Web server control:

  • Point to an external XSLT file, which will automatically apply the transformation to the XML document.
  • Apply a transformation that is an object of type XslTransform to the XML document.

Both methods have the same results, and your choice is dependent primarily on what is most convenient in your application. If the transformation is in the form of an .xsl or .xslt file, it is easy to load the file. If the transformation is in the form of an object — perhaps it is being passed to your application by another process — then you can apply it as an object.

Note   The XslTransform class also allows you to load an .xsl or .xslt file into the instance of the transformation.

To apply a transformation from a file

  1. Add an XML control to the Web Forms page. For details, see Adding XML Web Server Controls to a Web Forms Page.
  2. Set the XML control's TransformSource property to the path to the XSLT document.
    Note   You need to be sure that when your application runs, it has sufficient permissions to read the XML file. For details, see Access Permissions for Web Applications.

    The following example shows how you can apply a transformation from a file to an XML control called Xml1.

    'Visual Basic
    Xml1.TransformSource = "mystyle.xsl"
    
    //C#
    Xml1.TransformSource = "mystyle.xsl";
    

To apply a transformation from an XslTransform object

  1. Create an instance of the XslTransform class.
  2. Set the XML control's Transform property to the instance of the transformation.

    The following example shows how you can create an instance of the transformation class and use it to apply the transformation to an object. In this example, both the XML document and the transformation are read from files, but in a real application, both objects might come from another component. The transformation is applied as soon as the page loads.

    'Visual Basic
    Private Sub Page_Load(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles MyBase.Load
       Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
       doc.Load(Server.MapPath("MySource.xml"))
       Dim trans As System.Xml.Xsl.XslTransform = _
          New System.Xml.Xsl.XslTransform
       trans.Load(Server.MapPath("MyStyle.xsl"))
       Xml1.Document = doc
       Xml1.Transform = trans
    End Sub
    
    // C#
    private void Page_Load(object sender, System.EventArgs e)
    {
        System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        doc.Load(Server.MapPath("MySource.xml"));
        System.Xml.Xsl.XslTransform trans = new 
           System.Xml.Xsl.XslTransform();
        trans.Load(Server.MapPath("MyStyle.xsl"));
        Xml1.Document = doc;
        Xml1.Transform = trans;
    }
    

See Also

Introduction to the XML Web Server Control | Adding XML Web Server Controls to a Web Forms Page | Loading XML Data in the XML Web Server Control | Walkthrough: Displaying an XML Document in a Web Forms Page using Transformations

Show: