Sdílet prostřednictvím


How to: Transform XML Data in the XML Web Server Control

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. 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.

  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.

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

    Xml1.TransformSource = "mystyle.xsl"
    
    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 code 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.

    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
    
    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

Reference

XML Web Server Control Overview