XslCompiledTransform.Load 方法

定义

编译样式表。

重载

Load(IXPathNavigable, XsltSettings, XmlResolver)

编译 IXPathNavigable 中包含的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

Load(String, XsltSettings, XmlResolver)

加载和编译 URI 指定的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

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

从使用 XSLTC.exe 实用程序编译的样式表中加载一个方法。

Load(XmlReader, XsltSettings, XmlResolver)

编译 XmlReader 中包含的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

Load(XmlReader)

编译 XmlReader 中包含的样式表。

Load(Type)

加载使用 XSLT 编译器 (xsltc.exe) 创建的已编译样式表。

Load(String)

加载和编译位于指定 URI 的样式表。

Load(IXPathNavigable)

编译 IXPathNavigable 对象中包含的样式表。

注解

尽管 XslCompiledTransform 类的总体性能优于 XslTransform 类,但在首次对转换调用时,Load 类的 XslCompiledTransform 方法可能比 Load 类的 XslTransform 方法慢。 这是因为必须先编译 XSLT 文件,才能加载该文件。 有关详细信息,请参阅以下博客文章:XslCompiledTransform Slower than XslTransform?(XslCompiledTransform 比 XslTransform 慢?)

注意

以调试模式编译的 XSLT 与以发布模式编译的 XSLT 之间有一些差别。 在有些情况下,以调试模式编译的样式表在 Load 期间不会引发错误,但以后在 Transform 期间会失败。 以发布模式编译的同一个样式表在 Load 期间就会失败。 例如,当某个表达式需要节点集,而分配给该表达式的变量不是节点集类型的时,就会出现这种情况。

Load(IXPathNavigable, XsltSettings, XmlResolver)

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

编译 IXPathNavigable 中包含的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

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)

参数

stylesheet
IXPathNavigable

一个实现 IXPathNavigable 接口的对象。 在 Microsoft .NET Framework 中,这可以是 XmlNode(一般为 XmlDocument),或者是包含该样式表的 XPathDocument

settings
XsltSettings

应用于样式表的 XsltSettings。 如果是 null,则应用 Default 设置。

stylesheetResolver
XmlResolver

XmlResolver用于解析 XSLT importinclude 元素中引用的任何样式表的 。 如果这为 null,则不解析外部资源。

例外

stylesheet 值为 null

样式表中有错。

示例

以下示例加载样式表。 对象 XmlSecureResolver 包含访问在样式表中找到的任何 importinclude 元素所需的凭据。

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

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

另请参阅

适用于

Load(String, XsltSettings, XmlResolver)

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

加载和编译 URI 指定的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

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)

参数

stylesheetUri
String

样式表的 URI。

settings
XsltSettings

应用于样式表的 XsltSettings。 如果是 null,则应用 Default 设置。

stylesheetResolver
XmlResolver

XmlResolver用于解析样式表 URI 以及 XSLT importinclude 元素中引用的任何样式表的 。

例外

stylesheetUristylesheetResolver 值为 null

样式表中有错。

无法找到该样式表。

stylesheetUri 值包含无法找到的文件名或目录。

无法解析 stylesheetUri 值。

- 或 -

处理该请求时出错。

stylesheetUri 不是有效的 URI。

加载样式表时出现分析错误。

示例

以下示例加载存储在网络资源上的样式表。 XmlSecureResolver 对象指定访问该样式表所需的凭据。

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

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

XmlReader具有默认设置的 用于加载样式表。 在 上 XmlReader禁用 DTD 处理。 如果需要 DTD 处理,请创建 XmlReader 启用此功能的 ,并将其传递给 Load 方法。

另请参阅

适用于

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

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

从使用 XSLTC.exe 实用程序编译的样式表中加载一个方法。

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

参数

executeMethod
MethodInfo

一个 MethodInfo 对象,表示编译的样式表的编译器生成 execute 方法。

queryData
Byte[]

由 方法生成的CompileToType(XmlReader, XsltSettings, XmlResolver, Boolean, TypeBuilder, String)已编译样式表的 字段中的序列化数据结构的staticData字节数组。

earlyBoundTypes
Type[]

存储在已编译样式表的编译器生成的 ebTypes 字段中的类型数组。

示例

下面的代码示例使用 Load 加载已编译的样式表。 转换将元素的值 Price 减少 10%。

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

注意

此示例中使用的“转换”程序集是使用 xsltc.exe 实用工具生成的。 有关使用此命令行工具的详细信息,请参阅 如何:使用程序集执行 XSLT 转换

前面的代码示例使用以下转换:

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

注解

此方法接受对象、字节数组和类型数组形式的 MethodInfo 已编译样式表。 DynamicMethod 对象可用于允许在回收对象时 XslCompiledTransform 放弃已编译的样式表方法。

适用于

Load(XmlReader, XsltSettings, XmlResolver)

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

编译 XmlReader 中包含的 XSLT 样式表。 解析 XmlResolver 任何 XSLT importinclude 元素,XSLT 设置确定样式表的权限。

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)

参数

stylesheet
XmlReader

包含该样式表的 XmlReader

settings
XsltSettings

应用于样式表的 XsltSettings。 如果是 null,则应用 Default 设置。

stylesheetResolver
XmlResolver

XmlResolver用于解析 XSLT importinclude 元素中引用的任何样式表的 。 如果这为 null,则不解析外部资源。

例外

stylesheet 值为 null

样式表中有错。

示例

以下示例加载样式表并启用对 XSLT 脚本的支持。

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

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

样式表从 的 XmlReader 当前节点加载到其所有子级。 这使你能够将文档的一部分用作样式表。 Load方法完成后, XmlReader 将定位在样式表末尾后的下一个节点上。 如果已到达文档结尾,XmlReader 将位于文件结尾 (EOF)。

另请参阅

适用于

Load(XmlReader)

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

编译 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)

参数

stylesheet
XmlReader

包含此样式表的 XmlReader

例外

stylesheet 值为 null

样式表中有错。

示例

以下示例加载 对象中包含的 XmlReader 样式表。

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

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

样式表从 的 XmlReader 当前节点加载到其所有子级。 这使你能够将文档的一部分用作样式表。 Load方法完成后,XmlReader将定位在样式表末尾后的下一个节点上。 如果已到达文档结尾,XmlReader 将位于文件结尾 (EOF)。

此方法具有以下行为:

  • XmlUrlResolver没有用户凭据的 用于处理任何 xsl:importxsl:include 元素。

  • 函数 document() 已禁用。

  • 不支持嵌入脚本。

可以使用 Load(XmlReader, XsltSettings, XmlResolver) 重载来指定 XmlResolver 具有必要身份验证凭据的 ,或指定不同的 XSLT 设置。

另请参阅

适用于

Load(Type)

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

加载使用 XSLT 编译器 (xsltc.exe) 创建的已编译样式表。

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

参数

compiledStylesheet
Type

包含已编译样式表的类的名称。

该名称通常是样式表的名称。 除非另外指定,否则,xsltc.exe 工具将样式表名称用作类和程序集的名称。

示例

以下示例演示如何从 XSLT 程序集加载已编译的样式表。 该示例假定使用 xsltc.exe 工具创建名为 bookOrders.dll 的程序集,该程序集具有名为 bookOrders 的类。

注意

编译代码时必须引用 XSLT 程序集。 例如,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));

注解

xsltc.exe工具用于编译样式表并从样式表生成程序集。 方法 Load 从程序集加载已编译的样式表。

注意

还必须将 XSLT 程序集作为引用包含在应用程序中。

另请参阅

适用于

Load(String)

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

加载和编译位于指定 URI 的样式表。

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

参数

stylesheetUri
String

样式表的 URI。

例外

stylesheetUri 值为 null

样式表中有错。

无法找到该样式表。

stylesheetUri 值包含无法找到的文件名或目录。

无法解析 stylesheetUri 值。

- 或 -

处理该请求时出错。

stylesheetUri 不是有效的 URI。

加载样式表时出现分析错误。

示例

以下示例执行转换并输出到文件。

// 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")

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

此方法具有以下行为:

  • XmlUrlResolver不使用用户凭据的 用于解析样式表 URI,并处理任何 xsl:importxsl:include 元素。

  • XmlReader具有默认设置的 用于加载样式表。 在 上 XmlReader禁用 DTD 处理。 如果需要 DTD 处理,请创建 XmlReader 启用了此功能的 ,并将其传递给 Load 方法。

  • 函数 document() 已禁用。

  • 不支持嵌入脚本。

可以使用 Load(String, XsltSettings, XmlResolver) 重载来指定 XmlResolver 具有必要身份验证凭据的 ,或指定不同的 XSLT 设置。

另请参阅

适用于

Load(IXPathNavigable)

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

编译 IXPathNavigable 对象中包含的样式表。

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)

参数

stylesheet
IXPathNavigable

一个实现 IXPathNavigable 接口的对象。 在 Microsoft .NET Framework 中,这可以是 XmlNode(一般为 XmlDocument),或者是包含该样式表的 XPathDocument

例外

stylesheet 值为 null

样式表中有错。

示例

以下示例加载对象中包含的 XPathDocument 样式表。

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

注解

XslCompiledTransform 支持 XSLT 1.0 语法。 XSLT 样式表必须使用 http://www.w3.org/1999/XSL/Transform 命名空间。

此方法具有以下行为:

  • XmlUrlResolver没有用户凭据的 用于处理任何 xsl:importxsl:include 元素。

  • 函数 document() 已禁用。

  • 不支持嵌入脚本。

可以使用 Load 重载来指定 XmlResolver 具有必要身份验证凭据的 ,或指定不同的 XSLT 设置。

另请参阅

适用于