How to: Migrate Your XslTransform Code

The XslCompiledTransform class replaced the XslTransform class. This newer class was designed to be very similar to XslTransform. Style sheets are compiled using the Load method. Transforms are executed using the Transform method.

The following procedures show common XSLT tasks, and compare the code using the XslTransform class versus the XslCompiledTransform class.

To transform a file and output to a URI

  • Code using the XslTransform class.

    XslTransform xslt = new XslTransform();
    xslt.Load("output.xsl");
    xslt.Transform("books.xml", "books.html");
    
    Dim xslt As New XslTransform()
    xslt.Load("output.xsl")
    xslt.Transform("books.xml", "books.html")
    
  • Code using the XslCompiledTransform class.

    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("output.xsl");
    xslt.Transform("books.xml", "books.html");
    
    Dim xslt As New XslCompiledTransform()
    xslt.Load("output.xsl")
    xslt.Transform("books.xml", "books.html")
    

To compile a style sheet and use a resolver with default credentials

  • Code using the XslTransform class.

    XslTransform xslt = new XslTransform();
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    xslt.Load("sort.xsl", resolver);
    
    Dim xslt As New XslTransform()
    Dim resolver As New XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials
    xslt.Load("sort.xsl", resolver)
    
  • Code using the XslCompiledTransform class.

    XslCompiledTransform xslt = new XslCompiledTransform();
    XmlUrlResolver resolver = new XmlUrlResolver();
    resolver.Credentials = CredentialCache.DefaultCredentials;
    xslt.Load("sort.xsl", XsltSettings.Default, resolver);
    
    Dim xslt As New XslCompiledTransform()
    Dim resolver As New XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials
    xslt.Load("sort.xsl", XsltSettings.Default, resolver)
    

To use an XSLT parameter

  • Code using the XslTransform class.

    XslTransform xslt = new XslTransform();
    xslt.Load("order.xsl");
    
    //Create the XsltArgumentList.
    XsltArgumentList argList = new XsltArgumentList();
    
    //Create a parameter which represents the current date and time.
    DateTime d = DateTime.Now;
    argList.AddParam("date", "", d.ToString());
    
    //Create the XmlTextWriter.
    XmlTextWriter writer = new XmlTextWriter("output.xml", null);
    
    //Transform the file.
    xslt.Transform(new XPathDocument(filename), argList, writer, null);
    
    Dim xslt As New XslTransform()
    xslt.Load("order.xsl")
    
    'Create the XsltArgumentList.
    Dim argList As New XsltArgumentList()
    
    'Create a parameter which represents the current date and time.
    Dim d As DateTime = DateTime.Now
    argList.AddParam("date", "", d.ToString())
    
    'Create the XmlTextWriter.
    Dim writer As New XmlTextWriter("output.xml", Nothing)
    
    'Transform the file.
    xslt.Transform(New XPathDocument(filename), argList, writer, Nothing)
    
  • Code using the XslCompiledTransform class.

    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("order.xsl");
    
    // Create the XsltArgumentList.
    XsltArgumentList argList = new XsltArgumentList();
    
    // Create a parameter which represents the current date and time.
    DateTime d = DateTime.Now;
    argList.AddParam("date", "", d.ToString());
    
    // Create the XmlWriter.
    XmlWriter writer = XmlWriter.Create("output.xml", null);
    
    // Transform the file.
    xslt.Transform(new XPathDocument(filename), argList, writer);
    
    Dim xslt As New XslCompiledTransform()
    xslt.Load("order.xsl")
    
    ' Create the XsltArgumentList.
    Dim argList As New XsltArgumentList()
    
    ' Create a parameter which represents the current date and time.
    Dim d As DateTime = DateTime.Now
    argList.AddParam("date", "", d.ToString())
    
    ' Create the XmlWriter. 
    Dim writer As XmlWriter = XmlWriter.Create("output.xml", Nothing)
    
    ' Transform the file.
    xslt.Transform(New XPathDocument(filename), argList, writer)
    

To enable XSLT scripting

Note

Script blocks are supported only in .NET Framework. They are not supported on .NET Core or .NET 5 or later.

  • Code using the XslTransform class.

    XslTransform xslt = new XslTransform();
    xslt.Load("output.xsl");
    xslt.Transform("books.xml", "books.html");
    
    Dim xslt As New XslTransform()
    xslt.Load("output.xsl")
    xslt.Transform("books.xml", "books.html")
    
  • Code using the XslCompiledTransform class.

    // Create the XsltSettings object with script enabled.
    XsltSettings settings = new XsltSettings(false,true);
    
    // Execute the transform.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("calc.xsl", settings, new XmlUrlResolver());
    xslt.Transform("books.xml", "books.html");
    
    ' Create the XsltSettings object with script enabled.
    Dim settings As New XsltSettings(False, True)
    
    ' Execute the transform.
    Dim xslt As New XslCompiledTransform()
    xslt.Load("calc.xsl", settings, New XmlUrlResolver())
    xslt.Transform("books.xml", "books.html")
    

To load the results into a DOM object

  • Code using the XslTransform class.

    // Execute the transformation.
    XslTransform xslt = new XslTransform();
    xslt.Load("output.xsl");
    XmlReader reader = xslt.Transform(new XPathDocument("books.xml"), null, new XmlUrlResolver());
    
    // Load the results into a DOM object.
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);
    
    ' Execute the transformation.
    Dim xslt As New XslTransform()
    xslt.Load("output.xsl")
    Dim reader As XmlReader = xslt.Transform(New XPathDocument("books.xml"), Nothing, New XmlUrlResolver())
    
    ' Load the results into a DOM object.
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load(reader)
    
  • Code using the XslCompiledTransform class.

    Note

    The XslCompiledTransform class does not have a method that returns the XSLT transformation results as an XmlReader object. However, you can output to an XML file and load the XML file into another object.

    // Execute the transformation.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("output.xsl");
    xslt.Transform("books.xml", "output.xml");
    
    // Load the results into a DOM object.
    XmlDocument doc = new XmlDocument();
    doc.Load("output.xml");
    
    ' Execute the transformation.
    Dim xslt As New XslCompiledTransform()
    xslt.Load("output.xsl")
    xslt.Transform("books.xml", "output.xml")
    
    ' Load the results into a DOM object.
    Dim doc As XmlDocument = New XmlDocument()
    doc.Load("output.xml")
    

To stream the results into another data store

  • Code using the XslTransform class.

    // Execute the transformation.
    XslTransform xslt = new XslTransform();
    xslt.Load("output.xsl");
    XmlReader reader = xslt.Transform(new XPathDocument("books.xml"), null, new XmlUrlResolver());
    
    // Load the results into an XPathDocument object.
    XPathDocument doc = new XPathDocument(reader);
    
    ' Execute the transformation.
    Dim xslt As New XslTransform()
    xslt.Load("output.xsl")
    Dim reader As XmlReader = xslt.Transform(New XPathDocument("books.xml"), Nothing, New XmlUrlResolver())
    
    ' Load the results into an XPathDocument object.
    Dim doc As XPathDocument = New XPathDocument(reader)
    
  • Code using the XslCompiledTransform class.

    // Execute the transformation.
    XslCompiledTransform xslt = new XslCompiledTransform();
    xslt.Load("output.xsl");
    MemoryStream ms = new MemoryStream();
    xslt.Transform(new XPathDocument("books.xml"), null, ms);
    
    // Load the results into an XPathDocument object.
    ms.Seek(0, SeekOrigin.Begin);
    XPathDocument doc = new XPathDocument(ms);
    
    ' Execute the transformation.
    Dim xslt As New XslCompiledTransform()
    xslt.Load("output.xsl")
    Dim ms As MemoryStream = New MemoryStream()
    xslt.Transform(New XPathDocument("books.xml"), Nothing, ms)
    
    ' Load the results into an XPathDocument object.
    ms.Seek(0, SeekOrigin.Begin)
    Dim doc As XPathDocument = New XPathDocument(ms)
    

See also