Export (0) Print
Expand All

XslCompiledTransform.Transform Method (String, XsltArgumentList, XmlWriter)

Executes the transform using the input document specified by the URI and outputs the results to an XmlWriter. The XsltArgumentList provides additional run-time arguments.

Namespace:  System.Xml.Xsl
Assembly:  System.Xml (in System.Xml.dll)

public void Transform(
	string inputUri,
	XsltArgumentList arguments,
	XmlWriter results
)

Parameters

inputUri
Type: System.String

The URI of the input document.

arguments
Type: System.Xml.Xsl.XsltArgumentList

An XsltArgumentList containing the namespace-qualified arguments used as input to the transform. This value can be null.

results
Type: System.Xml.XmlWriter

The XmlWriter to which you want to output.

If the style sheet contains an xsl:output element, you should create the XmlWriter using the XmlWriterSettings object returned from the OutputSettings property. This ensures that the XmlWriter has the correct output settings.

ExceptionCondition
ArgumentNullException

The inputUri or results value is null.

XsltException

There was an error executing the XSLT transform.

DirectoryNotFoundException

The inputtUri value includes a filename or directory cannot be found.

WebException

The inputUri value cannot be resolved.

-or-

An error occurred while processing the request.

UriFormatException

inputUri is not a valid URI.

XmlException

There was a parsing error loading the input document.

This method uses a default XmlUrlResolver with no user credentials to resolve the input document and any instances of the XSLT document() function found in the style sheet. If any of these resources are located on a network resource that requires authentication, use the overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

An XmlReader with default settings is used to load the input document. DTD processing is disabled on the XmlReader. If you require DTD processing, create an XmlReader with this feature enabled, and pass it to the Transform method.

The following example uses an XsltArgumentList object to create a parameter representing the current date and time.

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

public class Sample
{

    public static void Main()
    {

        // Create the XslCompiledTransform and load the stylesheet.
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load("order.xsl");

        // Create the XsltArgumentList.
        XsltArgumentList xslArg = new XsltArgumentList();

        // Create a parameter which represents the current date and time.
        DateTime d = DateTime.Now;
        xslArg.AddParam("date", "", d.ToString());

        // Transform the file. 
        using (XmlWriter w = XmlWriter.Create("output.xml"))
        {
            xslt.Transform("order.xml", xslArg, w);
        }
    }
}

The example uses the following two data files as input.

order.xml

<!--Represents a customer order-->
<order>
  <book ISBN='10-861003-324'>
    <title>The Handmaid's Tale</title>
    <price>19.95</price>
  </book>
  <cd ISBN='2-3631-4'>
    <title>Americana</title>
    <price>16.95</price>
  </cd>
</order>

order.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:param name="date"/>
  <xsl:template match="/">
    <order>
      <date><xsl:value-of select="$date"/></date>
      <total><xsl:value-of select="sum(//price)"/></total>
    </order>
  </xsl:template>
</xsl:stylesheet>

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft