Читати англійською Редагувати

Поділитися через


XslCompiledTransform.Transform Method

Definition

Executes the XSLT transformation.

Overloads

Transform(XmlReader, XsltArgumentList, TextWriter)

Executes the transform using the input document specified by the XmlReader object and outputs the results to a TextWriter. The XsltArgumentList provides additional run-time arguments.

Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

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

Transform(IXPathNavigable, XsltArgumentList, TextWriter)

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

Transform(IXPathNavigable, XsltArgumentList, Stream)

Executes the transform using the input document specified by the IXPathNavigable object and outputs the results to a stream. The XsltArgumentList provides additional runtime arguments.

Transform(XmlReader, XsltArgumentList, XmlWriter)

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

Transform(XmlReader, XsltArgumentList, Stream)

Executes the transform using the input document specified by the XmlReader object and outputs the results to a stream. The XsltArgumentList provides additional run-time arguments.

Transform(String, String)

Executes the transform using the input document specified by the URI and outputs the results to a file.

Transform(String, XsltArgumentList, TextWriter)

Executes the transform using the input document specified by the URI and outputs the results to a TextWriter.

Transform(String, XsltArgumentList, Stream)

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

Transform(IXPathNavigable, XmlWriter)

Executes the transform using the input document specified by the IXPathNavigable object and outputs the results to an XmlWriter.

Transform(XmlReader, XmlWriter)

Executes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter.

Transform(String, XmlWriter)

Executes the transform using the input document specified by the URI and outputs the results to an XmlWriter.

Transform(XmlReader, XsltArgumentList, XmlWriter, XmlResolver)

Executes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter. The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

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

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

Executes the transform by using the input document that is specified by the IXPathNavigable object and outputs the results to an XmlWriter. The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

Remarks

Примітка

There are differences between XSLT compiled in Debug mode and XSLT compiled in Release mode. In some situations, style sheets compiled in Debug mode will not throw errors during Load, but will later fail during Transform. The same style sheet compiled in Release mode will fail during Load. An example of such behavior is when a variable that is not of a node-set type is assigned to an expression where a node-set is required.

Transform(XmlReader, XsltArgumentList, TextWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the XmlReader object and outputs the results to a TextWriter. The XsltArgumentList provides additional run-time arguments.

C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.TextWriter results);
C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results);

Parameters

input
XmlReader

An XmlReader containing the input document.

arguments
XsltArgumentList

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

results
TextWriter

The TextWriter to which you want to output.

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Remarks

If the style sheet includes an XSLT document() function, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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

C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? arguments, System.Xml.XmlWriter results);
C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results);

Parameters

input
IXPathNavigable

An object implementing the IXPathNavigable interface. In the Microsoft .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

arguments
XsltArgumentList

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

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

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Remarks

If the XSLT document() function is enabled, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

This method does not support the xsl:strip-space element. If your compiled style sheet included the xsl:strip-space element, you must use the Transform overload which takes an XmlReader as the input argument.

The IXPathNavigable interface is implemented in the XmlNode and XPathDocument classes. These classes represent an in-memory cache of XML data.

  • The XmlNode class is based on the W3C Document Object Model (DOM) and includes editing capabilities.

  • The XPathDocument class is a read-only data store based on the XPath data model. XPathDocument is the recommended class for XSLT processing. It provides faster performance when compared to the XmlNode class.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an object containing just the node fragment, and pass that object to the Transform method. For more information, see How to: Transform a Node Fragment.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, TextWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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

C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.TextWriter results);
C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results);

Parameters

input
IXPathNavigable

An object implementing the IXPathNavigable interface. In the Microsoft .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

arguments
XsltArgumentList

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

results
TextWriter

The TextWriter to which you want to output.

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Remarks

If the XSLT document() function is enabled, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

This method does not support the xsl:strip-space element. If your compiled style sheet included the xsl:strip-space element, you must use the Transform overload which takes an XmlReader as the input argument.

The IXPathNavigable interface is implemented in the XmlNode and XPathDocument classes. These classes represent an in-memory cache of XML data.

  • The XmlNode class is based on the W3C Document Object Model (DOM) and includes editing capabilities.

  • The XPathDocument class is a read-only data store based on the XPath data model. XPathDocument is the recommended class for XSLT processing. It provides faster performance when compared to the XmlNode class.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an object containing just the node fragment, and pass that object to the Transform method. For more information, see How to: Transform a Node Fragment.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, Stream)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the IXPathNavigable object and outputs the results to a stream. The XsltArgumentList provides additional runtime arguments.

C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.Stream results);
C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results);

Parameters

input
IXPathNavigable

An object implementing the IXPathNavigable interface. In the Microsoft .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

arguments
XsltArgumentList

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

results
Stream

The stream to which you want to output.

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Examples

The following example executes an XSLT transformation and outputs to a FileStream.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Create the FileStream.
using (FileStream fs = new FileStream(@"c:\data\output.xml", FileMode.Create))
{
   // Execute the transformation.
   xslt.Transform(new XPathDocument("books.xml"), null, fs);
}

Remarks

If the XSLT document() function is enabled, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

This method does not support the xsl:strip-space element. If your compiled style sheet included the xsl:strip-space element, you must use the Transform overload which takes an XmlReader as the input argument.

The IXPathNavigable interface is implemented in the XmlNode and XPathDocument classes. These classes represent an in-memory cache of XML data.

  • The XmlNode class is based on the W3C Document Object Model (DOM) and includes editing capabilities.

  • The XPathDocument class is a read-only data store based on the XPath data model. XPathDocument is the recommended class for XSLT processing. It provides faster performance when compared to the XmlNode class.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an object containing just the node fragment, and pass that object to the Transform method. For more information, see How to: Transform a Node Fragment.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XmlReader, XsltArgumentList, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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

C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList? arguments, System.Xml.XmlWriter results);
C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results);

Parameters

input
XmlReader

An XmlReader containing the input document.

arguments
XsltArgumentList

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

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

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Remarks

If the style sheet includes an XSLT document() function, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XmlReader, XsltArgumentList, Stream)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the XmlReader object and outputs the results to a stream. The XsltArgumentList provides additional run-time arguments.

C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.Stream results);
C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results);

Parameters

input
XmlReader

An XmlReader containing the input document.

arguments
XsltArgumentList

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

results
Stream

The stream to which you want to output.

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Remarks

If the style sheet includes an XSLT document() function, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(String, String)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the URI and outputs the results to a file.

C#
public void Transform(string inputUri, string resultsFile);

Parameters

inputUri
String

The URI of the input document.

resultsFile
String

The URI of the output file.

Exceptions

The inputUri or resultsFile value is null.

There was an error executing the XSLT transform.

The input document cannot be found.

The inputUri or resultsFile value includes a filename or directory cannot be found.

The inputUri or resultsFile value cannot be resolved.

-or-

An error occurred while processing the request.

inputUri or resultsFile is not a valid URI.

There was a parsing error loading the input document.

Examples

The following example executes a transform and outputs to a file.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html");

The sample uses the following two input files:

books.xml

XML
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

output.xsl

XML
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
  <HTML>
    <BODY>
      <TABLE BORDER="2">
        <TR>
          <TD>ISBN</TD>
          <TD>Title</TD>
          <TD>Price</TD>
        </TR>
        <xsl:apply-templates select="book"/>
      </TABLE>
    </BODY>
  </HTML>
</xsl:template>
<xsl:template match="book">
  <TR>
    <TD><xsl:value-of select="@ISBN"/></TD>
    <TD><xsl:value-of select="title"/></TD>
    <TD><xsl:value-of select="price"/></TD>
  </TR>
</xsl:template>
</xsl:stylesheet>

Remarks

This method uses a default XmlUrlResolver with no user credentials to resolve the input and output documents. 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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(String, XsltArgumentList, TextWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the URI and outputs the results to a TextWriter.

C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.TextWriter results);
C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.IO.TextWriter results);

Parameters

inputUri
String

The URI of the input document.

arguments
XsltArgumentList

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

results
TextWriter

The TextWriter to which you want to output.

Exceptions

The inputUri or results value is null.

There was an error executing the XSLT transform.

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

The inputUri value cannot be resolved.

-or-

An error occurred while processing the request.

inputUri is not a valid URI.

There was a parsing error loading the input document.

Examples

The following example executes an XSLT transformation and outputs a string.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("HTML_out.xsl");

// Transform the file and output an HTML string.
string HTMLoutput;
StringWriter writer = new StringWriter();
xslt.Transform("books.xml", null, writer);
HTMLoutput = writer.ToString();
writer.Close();

Remarks

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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(String, XsltArgumentList, Stream)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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

C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList? arguments, System.IO.Stream results);
C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.IO.Stream results);

Parameters

inputUri
String

The URI of the input document.

arguments
XsltArgumentList

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

results
Stream

The stream to which you want to output.

Exceptions

The inputUri or results value is null.

There was an error executing the XSLT transform.

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

The inputUri value cannot be resolved.

-or-

An error occurred while processing the request.

inputUri is not a valid URI.

There was a parsing error loading the input document.

Remarks

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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the IXPathNavigable object and outputs the results to an XmlWriter.

C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.XmlWriter results);

Parameters

input
IXPathNavigable

An object implementing the IXPathNavigable interface. In the Microsoft .NET Framework, this can be either an XmlNode (typically an XmlDocument), or an XPathDocument containing the data to be transformed.

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

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Examples

The following example executes an XSLT transformation and outputs to an XmlWriter object.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create("output.xml", settings);

// Execute the transformation.
xslt.Transform(new XPathDocument("books.xml"), writer);
writer.Close();

Remarks

If the XSLT document() function is enabled, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

This method does not support the xsl:strip-space element. If your compiled style sheet included the xsl:strip-space element, you must use the Transform overload which takes an XmlReader as the input argument.

The IXPathNavigable interface is implemented in the XmlNode and XPathDocument classes. These classes represent an in-memory cache of XML data.

  • The XmlNode class is based on the W3C Document Object Model (DOM) and includes editing capabilities.

  • The XPathDocument class is a read-only data store based on the XPath data model. XPathDocument is the recommended class for XSLT processing. It provides faster performance when compared to the XmlNode class.

Transformations apply to the document as a whole. In other words, if you pass in a node other than the document root node, this does not prevent the transformation process from accessing all nodes in the loaded document. To transform a node fragment, you must create an object containing just the node fragment, and pass that object to the Transform method. For more information, see How to: Transform a Node Fragment.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XmlReader, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter.

C#
public void Transform(System.Xml.XmlReader input, System.Xml.XmlWriter results);

Parameters

input
XmlReader

The XmlReader containing the input document.

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

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Examples

The following example transforms the first book in the books.xml file.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create("output.xml", settings);

XmlReader reader = XmlReader.Create("books.xml");
reader.MoveToContent();

// Execute the transformation.
xslt.Transform(reader, writer);
writer.Close();
reader.Close();

Remarks

If the XSLT document() function is enabled, it is resolved using an XmlUrlResolver with no user credentials. If the external resource is 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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(String, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the URI and outputs the results to an XmlWriter.

C#
public void Transform(string inputUri, System.Xml.XmlWriter results);

Parameters

inputUri
String

The URI of the input document.

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

Exceptions

The inputUri or results value is null.

There was an error executing the XSLT transform.

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

The inputUri value cannot be resolved.

-or-

An error occurred while processing the request.

inputUri is not a valid URI.

There was a parsing error loading the input document.

Examples

The following example executes an XSLT transformation and outputs to an XmlWriter object.

C#
// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create("output.xml", settings);

// Execute the transformation.
xslt.Transform("books.xml", writer);
writer.Close();

Remarks

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.

This method does not support the xsl:strip-space element. If your compiled style sheet included the xsl:strip-space element, you must use the Transform overload which takes an XmlReader as the input argument.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(XmlReader, XsltArgumentList, XmlWriter, XmlResolver)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform using the input document specified by the XmlReader object and outputs the results to an XmlWriter. The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList? arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver? documentResolver);
C#
public void Transform(System.Xml.XmlReader input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver documentResolver);

Parameters

input
XmlReader

An XmlReader containing the input document.

arguments
XsltArgumentList

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

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

documentResolver
XmlResolver

The XmlResolver used to resolve the XSLT document() function. If this is null, the document() function is not resolved.

Exceptions

The input or results value is null.

There was an error executing the XSLT transform.

Examples

The following example uses an XmlSecureResolver to resolve the XSLT document() function.

C#
// Create a resolver and specify the necessary credentials.
XmlSecureResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), "http://serverName/data/");
System.Net.NetworkCredential myCred;
myCred  = new System.Net.NetworkCredential(UserName,SecurelyStoredPassword,Domain);
resolver.Credentials = myCred;

XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("http://serverName/data/xsl/sort.xsl", settings, resolver);

// Transform the file.
using (XmlReader reader = XmlReader.Create("books.xml"))
{
   using (XmlWriter writer = XmlWriter.Create("output.xml"))
   {
      xslt.Transform(reader, null, writer, resolver);
   }
}

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(String, XsltArgumentList, XmlWriter)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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.

C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList? arguments, System.Xml.XmlWriter results);
C#
public void Transform(string inputUri, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results);

Parameters

inputUri
String

The URI of the input document.

arguments
XsltArgumentList

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

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

Exceptions

The inputUri or results value is null.

There was an error executing the XSLT transform.

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

The inputUri value cannot be resolved.

-or-

An error occurred while processing the request.

inputUri is not a valid URI.

There was a parsing error loading the input document.

Examples

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

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

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

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

Remarks

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.

See also

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Transform(IXPathNavigable, XsltArgumentList, XmlWriter, XmlResolver)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

Executes the transform by using the input document that is specified by the IXPathNavigable object and outputs the results to an XmlWriter. The XsltArgumentList provides additional run-time arguments and the XmlResolver resolves the XSLT document() function.

C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList? arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver? documentResolver);
C#
public void Transform(System.Xml.XPath.IXPathNavigable input, System.Xml.Xsl.XsltArgumentList arguments, System.Xml.XmlWriter results, System.Xml.XmlResolver documentResolver);

Parameters

input
IXPathNavigable

The document to transform that is specified by the IXPathNavigable object.

arguments
XsltArgumentList

Argument list as XsltArgumentList.

results
XmlWriter

The XmlWriter to which you want to output.

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

documentResolver
XmlResolver

The XmlResolver used to resolve the XSLT document() function. If this is null, the document() function is not resolved.

Applies to

.NET 10 та інші версії
Продукт Версії
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1