Bewerken

Share via


XslCompiledTransform.Load Method

Definition

Compiles the style sheet.

Overloads

Load(IXPathNavigable, XsltSettings, XmlResolver)

Compiles the XSLT style sheet contained in the IXPathNavigable. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

Load(String, XsltSettings, XmlResolver)

Loads and compiles the XSLT style sheet specified by the URI. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

Load(MethodInfo, Byte[], Type[])

Loads a method from a style sheet compiled using the XSLTC.exe utility.

Load(XmlReader, XsltSettings, XmlResolver)

Compiles the XSLT style sheet contained in the XmlReader. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

Load(XmlReader)

Compiles the style sheet contained in the XmlReader.

Load(Type)

Loads the compiled style sheet that was created using the XSLT Compiler (xsltc.exe).

Load(String)

Loads and compiles the style sheet located at the specified URI.

Load(IXPathNavigable)

Compiles the style sheet contained in the IXPathNavigable object.

Remarks

Although the overall performance of the XslCompiledTransform class is better than the XslTransform class, the Load method of the XslCompiledTransform class might perform more slowly than the Load method of the XslTransform class the first time it is called on a transformation. This is because the XSLT file must be compiled before it is loaded. For more information, see the following blog post: XslCompiledTransform Slower than XslTransform?

Note

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.

Load(IXPathNavigable, XsltSettings, XmlResolver)

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

Compiles the XSLT style sheet contained in the IXPathNavigable. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

public:
 void Load(System::Xml::XPath::IXPathNavigable ^ stylesheet, System::Xml::Xsl::XsltSettings ^ settings, System::Xml::XmlResolver ^ stylesheetResolver);
public void Load (System.Xml.XPath.IXPathNavigable stylesheet, System.Xml.Xsl.XsltSettings? settings, System.Xml.XmlResolver? stylesheetResolver);
public void Load (System.Xml.XPath.IXPathNavigable stylesheet, System.Xml.Xsl.XsltSettings settings, System.Xml.XmlResolver stylesheetResolver);
member this.Load : System.Xml.XPath.IXPathNavigable * System.Xml.Xsl.XsltSettings * System.Xml.XmlResolver -> unit
Public Sub Load (stylesheet As IXPathNavigable, settings As XsltSettings, stylesheetResolver As XmlResolver)

Parameters

stylesheet
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 style sheet.

settings
XsltSettings

The XsltSettings to apply to the style sheet. If this is null, the Default setting is applied.

stylesheetResolver
XmlResolver

The XmlResolver used to resolve any style sheets referenced in XSLT import and include elements. If this is null, external resources are not resolved.

Exceptions

The stylesheet value is null.

The style sheet contains an error.

Examples

The following example loads a style sheet. The XmlSecureResolver object contains the credentials necessary to access any import or include elements found in the style sheet.

// Create a resolver and specify the necessary credentials.
XmlUrlResolver resolver = new XmlUrlResolver();
System.Net.NetworkCredential myCred;
myCred  = new System.Net.NetworkCredential(UserName,SecurelyStoredPassword,Domain);
resolver.Credentials = myCred;

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(new XPathDocument("http://serverName/data/xsl/sort.xsl"), XsltSettings.Default, resolver);
' Create a resolver and specify the necessary credentials.
Dim resolver As New XmlUrlResolver()
Dim myCred As System.Net.NetworkCredential
myCred = New System.Net.NetworkCredential(UserName, SecurelyStoredPassword, Domain)
resolver.Credentials = myCred
        
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load(New XPathDocument("http://serverName/data/xsl/sort.xsl"), XsltSettings.Default, resolver)

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

See also

Applies to

Load(String, XsltSettings, XmlResolver)

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

Loads and compiles the XSLT style sheet specified by the URI. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

public:
 void Load(System::String ^ stylesheetUri, System::Xml::Xsl::XsltSettings ^ settings, System::Xml::XmlResolver ^ stylesheetResolver);
public void Load (string stylesheetUri, System.Xml.Xsl.XsltSettings? settings, System.Xml.XmlResolver? stylesheetResolver);
public void Load (string stylesheetUri, System.Xml.Xsl.XsltSettings settings, System.Xml.XmlResolver stylesheetResolver);
member this.Load : string * System.Xml.Xsl.XsltSettings * System.Xml.XmlResolver -> unit
Public Sub Load (stylesheetUri As String, settings As XsltSettings, stylesheetResolver As XmlResolver)

Parameters

stylesheetUri
String

The URI of the style sheet.

settings
XsltSettings

The XsltSettings to apply to the style sheet. If this is null, the Default setting is applied.

stylesheetResolver
XmlResolver

The XmlResolver used to resolve the style sheet URI and any style sheets referenced in XSLT import and include elements.

Exceptions

The stylesheetUri or stylesheetResolver value is null.

The style sheet contains an error.

The style sheet cannot be found.

The stylesheetUri value includes a filename or directory that cannot be found.

The stylesheetUri value cannot be resolved.

-or-

An error occurred while processing the request.

stylesheetUri is not a valid URI.

There was a parsing error loading the style sheet.

Examples

The following example loads a style sheet that is stored on a network resource. An XmlSecureResolver object specifies the credentials necessary to access the style sheet.

// Create the XslCompiledTransform object.
XslCompiledTransform xslt = new XslCompiledTransform();

// Create a resolver and set the credentials to use.
XmlSecureResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), "http://serverName/data/");
resolver.Credentials = CredentialCache.DefaultCredentials;

// Load the style sheet.
xslt.Load("http://serverName/data/xsl/sort.xsl", null, resolver);
' Create the XslCompiledTransform object.
Dim xslt As New XslCompiledTransform()
        
' Create a resolver and set the credentials to use.
Dim resolver As New XmlSecureResolver(New XmlUrlResolver(), "http://serverName/data/")
resolver.Credentials = CredentialCache.DefaultCredentials
        
' Load the style sheet.
xslt.Load("http://serverName/data/xsl/sort.xsl", Nothing, resolver)

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

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

See also

Applies to

Load(MethodInfo, Byte[], Type[])

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

Loads a method from a style sheet compiled using the XSLTC.exe utility.

public:
 void Load(System::Reflection::MethodInfo ^ executeMethod, cli::array <System::Byte> ^ queryData, cli::array <Type ^> ^ earlyBoundTypes);
public void Load (System.Reflection.MethodInfo executeMethod, byte[] queryData, Type[]? earlyBoundTypes);
public void Load (System.Reflection.MethodInfo executeMethod, byte[] queryData, Type[] earlyBoundTypes);
member this.Load : System.Reflection.MethodInfo * byte[] * Type[] -> unit
Public Sub Load (executeMethod As MethodInfo, queryData As Byte(), earlyBoundTypes As Type())

Parameters

executeMethod
MethodInfo

A MethodInfo object representing the compiler-generated execute method of the compiled style sheet.

queryData
Byte[]

A byte array of serialized data structures in the staticData field of the compiled style sheet as generated by the CompileToType(XmlReader, XsltSettings, XmlResolver, Boolean, TypeBuilder, String) method.

earlyBoundTypes
Type[]

An array of types stored in the compiler-generated ebTypes field of the compiled style sheet.

Examples

The code example below uses the Load to load a compiled style sheet. The transformation reduces the value of the Price element by ten percent.

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

class Example
{
    static void Main()
    {
        // Load a stylesheet compiled using the XSLTC.EXE utility
        Type compiledStylesheet = Assembly.Load("Transform").GetType("Transform");

        // Extract private members from the compiled stylesheet
        BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Static;
        MethodInfo executeMethod = compiledStylesheet.GetMethod("Execute", bindingFlags);
        object staticData = compiledStylesheet.GetField("staticData", bindingFlags).GetValue(null);
        object earlyBoundTypes = compiledStylesheet.GetField("ebTypes", bindingFlags).GetValue(null);

        // Load into XslCompiledTransform
        XslCompiledTransform xslt = new XslCompiledTransform();
        xslt.Load(executeMethod, (byte[])staticData, (Type[])earlyBoundTypes);

        // Run the transformation
        xslt.Transform(XmlReader.Create(new StringReader("<Root><Price>9.50</Price></Root>")), (XsltArgumentList)null, Console.Out);
    }
}
Imports System.IO
Imports System.Reflection
Imports System.Xml
Imports System.Xml.Xsl

Module Module1

    Sub Main()
        ' Load a stylesheet compiled using the XSLTC.EXE utility
        Dim compiledStylesheet As Type = [Assembly].Load("Transform").GetType("Transform")

        ' Extract private members from the compiled stylesheet
        Dim bindingFlags As BindingFlags = bindingFlags.NonPublic Or bindingFlags.Static
        Dim executeMethod As MethodInfo = compiledStylesheet.GetMethod("Execute", bindingFlags)
        Dim staticData As Object = compiledStylesheet.GetField("staticData", bindingFlags).GetValue(Nothing)
        Dim earlyBoundTypes As Object = compiledStylesheet.GetField("ebTypes", bindingFlags).GetValue(Nothing)

        ' Load into XslCompiledTransform
        Dim xslt As New XslCompiledTransform()
        xslt.Load(executeMethod, CType(staticData, Byte()), CType(earlyBoundTypes, Type()))

        ' Run the transformation
        xslt.Transform(XmlReader.Create(New StringReader("<Root><Price>9.50</Price></Root>")), CType(Nothing, XsltArgumentList), Console.Out)
    End Sub
End Module

Note

The "Transform" assembly used in this example was built using the xsltc.exe utility. For detailed information on using this command line tool, see How to: Perform an XSLT Transformation by Using an Assembly.

The previous code example uses the following transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  xmlns:user="urn:my-scripts">
  <msxsl:script language="C#" implements-prefix="user">
    <![CDATA[
  public double modifyPrice(double price){
    price*=0.9;
    return price;
  }
  ]]>
  </msxsl:script>
  <xsl:template match="Root">
    <Root xmlns="">
      <Price><xsl:value-of select="user:modifyPrice(Price)"/></Price>
    </Root>
  </xsl:template>
</xsl:stylesheet>

Remarks

This method accepts a compiled style sheet in the form of a MethodInfo object, a byte array, and a type array. DynamicMethod objects may be used to allow compiled style sheet methods to be discarded when the XslCompiledTransform object is reclaimed.

Applies to

Load(XmlReader, XsltSettings, XmlResolver)

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

Compiles the XSLT style sheet contained in the XmlReader. The XmlResolver resolves any XSLT import or include elements and the XSLT settings determine the permissions for the style sheet.

public:
 void Load(System::Xml::XmlReader ^ stylesheet, System::Xml::Xsl::XsltSettings ^ settings, System::Xml::XmlResolver ^ stylesheetResolver);
public void Load (System.Xml.XmlReader stylesheet, System.Xml.Xsl.XsltSettings? settings, System.Xml.XmlResolver? stylesheetResolver);
public void Load (System.Xml.XmlReader stylesheet, System.Xml.Xsl.XsltSettings settings, System.Xml.XmlResolver stylesheetResolver);
member this.Load : System.Xml.XmlReader * System.Xml.Xsl.XsltSettings * System.Xml.XmlResolver -> unit
Public Sub Load (stylesheet As XmlReader, settings As XsltSettings, stylesheetResolver As XmlResolver)

Parameters

stylesheet
XmlReader

The XmlReader containing the style sheet.

settings
XsltSettings

The XsltSettings to apply to the style sheet. If this is null, the Default setting is applied.

stylesheetResolver
XmlResolver

The XmlResolver used to resolve any style sheets referenced in XSLT import and include elements. If this is null, external resources are not resolved.

Exceptions

The stylesheet value is null.

The style sheet contains an error.

Examples

The following example loads a style sheet and enables support for XSLT scripting.

// Create the XslCompiledTransform object.
XslCompiledTransform xslt = new XslCompiledTransform();

// Create a resolver and set the credentials to use.
XmlSecureResolver resolver = new XmlSecureResolver(new XmlUrlResolver(), "http://serverName/data/");
resolver.Credentials = CredentialCache.DefaultCredentials;

XmlReader reader = XmlReader.Create("http://serverName/data/xsl/sort.xsl");

// Create the XsltSettings object with script enabled.
XsltSettings settings = new XsltSettings(false,true);

// Load the style sheet.
xslt.Load(reader, settings, resolver);
' Create the XslCompiledTransform object.
Dim xslt As New XslCompiledTransform()
        
' Create a resolver and set the credentials to use.
Dim resolver As New XmlSecureResolver(New XmlUrlResolver(), "http://serverName/data/")
resolver.Credentials = CredentialCache.DefaultCredentials
        
Dim reader As XmlReader = XmlReader.Create("http://serverName/data/xsl/sort.xsl")
        
' Create the XsltSettings object with script enabled.
Dim settings As New XsltSettings(False, True)
        
' Load the style sheet.
xslt.Load(reader, settings, resolver)

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

The style sheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the style sheet. After the Load method completes, the XmlReader is positioned on the next node after the end of the style sheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).

See also

Applies to

Load(XmlReader)

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

Compiles the style sheet contained in the XmlReader.

public:
 void Load(System::Xml::XmlReader ^ stylesheet);
public void Load (System.Xml.XmlReader stylesheet);
member this.Load : System.Xml.XmlReader -> unit
Public Sub Load (stylesheet As XmlReader)

Parameters

stylesheet
XmlReader

An XmlReader containing the style sheet.

Exceptions

The stylesheet value is null.

The style sheet contains an error.

Examples

The following example loads a style sheet contained in an XmlReader object.

// Create a reader that contains the style sheet.
XmlReader reader = XmlReader.Create("titles.xsl");
reader.ReadToDescendant("xsl:stylesheet");

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(reader);
' Create a reader that contains the style sheet.
Dim reader As XmlReader = XmlReader.Create("titles.xsl")
reader.ReadToDescendant("xsl:stylesheet")
        
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load(reader)

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

The style sheet loads from the current node of the XmlReader through all its children. This enables you to use a portion of a document as the style sheet. After the Load method completes, the XmlReader is positioned on the next node after the end of the style sheet. If the end of the document is reached, the XmlReader is positioned at the end of file (EOF).

This method has the following behavior:

  • An XmlUrlResolver with no user credentials is used to process any xsl:import or xsl:include elements.

  • The document() function is disabled.

  • Embedded scripts are not supported.

You can use the Load(XmlReader, XsltSettings, XmlResolver) overload to specify an XmlResolver with the necessary authentication credentials, or to specify different XSLT settings.

See also

Applies to

Load(Type)

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

Loads the compiled style sheet that was created using the XSLT Compiler (xsltc.exe).

public:
 void Load(Type ^ compiledStylesheet);
public void Load (Type compiledStylesheet);
member this.Load : Type -> unit
Public Sub Load (compiledStylesheet As Type)

Parameters

compiledStylesheet
Type

The name of the class that contains the compiled style sheet.

This is usually the name of the style sheet. Unless otherwise specified, the xsltc.exe tool uses the name of the style sheet for the class and assembly names.

Examples

The following example shows how to load a compiled style sheet from an XSLT assembly. The example assumes that the xsltc.exe tool was used to create an assembly named bookOrders.dll with a class named bookOrders.

Note

You have to reference the XSLT assembly when compiling the code. For example, csc /r:system.dll;system.xml.dll;bookOrders.dll myCode.cs.

// Load the type of the class.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(typeof(bookOrders));

Remarks

The xsltc.exe tool is used to compile style sheets and generate assemblies from the style sheets. The Load method loads the compiled style sheet from the assembly.

Note

You must also include the XSLT assembly as a reference in your application.

See also

Applies to

Load(String)

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

Loads and compiles the style sheet located at the specified URI.

public:
 void Load(System::String ^ stylesheetUri);
public void Load (string stylesheetUri);
member this.Load : string -> unit
Public Sub Load (stylesheetUri As String)

Parameters

stylesheetUri
String

The URI of the style sheet.

Exceptions

The stylesheetUri value is null.

The style sheet contains an error.

The style sheet cannot be found.

The stylesheetUri value includes a filename or directory that cannot be found.

The stylesheetUri value cannot be resolved.

-or-

An error occurred while processing the request.

stylesheetUri is not a valid URI.

There was a parsing error loading the style sheet.

Examples

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

// 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");
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load("output.xsl")
        
' Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html")

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

This method has the following behavior:

  • An XmlUrlResolver with no user credentials is used to resolve the style sheet URI, and process any xsl:import or xsl:include elements.

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

  • The document() function is disabled.

  • Embedded scripts are not supported.

You can use the Load(String, XsltSettings, XmlResolver) overload to specify an XmlResolver with the necessary authentication credentials, or to specify different XSLT settings.

See also

Applies to

Load(IXPathNavigable)

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

Compiles the style sheet contained in the IXPathNavigable object.

public:
 void Load(System::Xml::XPath::IXPathNavigable ^ stylesheet);
public void Load (System.Xml.XPath.IXPathNavigable stylesheet);
member this.Load : System.Xml.XPath.IXPathNavigable -> unit
Public Sub Load (stylesheet As IXPathNavigable)

Parameters

stylesheet
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 style sheet.

Exceptions

The stylesheet value is null.

The style sheet contains an error.

Examples

The following example loads a style sheet contained in an XPathDocument object.

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(new XPathDocument("http://serverName/data/xsl/sort.xsl"));
Dim xslt As New XslCompiledTransform()
xslt.Load(New XPathDocument("http://serverName/data/xsl/sort.xsl"))

Remarks

The XslCompiledTransform class supports the XSLT 1.0 syntax. The XSLT style sheet must use the http://www.w3.org/1999/XSL/Transform namespace.

This method has the following behavior:

  • An XmlUrlResolver with no user credentials is used to process any xsl:import or xsl:include elements.

  • The document() function is disabled.

  • Embedded scripts are not supported.

You can use the Load overload to specify an XmlResolver with the necessary authentication credentials, or to specify different XSLT settings.

See also

Applies to