通过


XslCompiledTransform.Load 方法

定义

编译样式表。

重载

名称 说明
Load(IXPathNavigable, XsltSettings, XmlResolver)

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

Load(String, XsltSettings, XmlResolver)

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

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

从使用实用工具编译的 XSLTC.exe 样式表加载方法。

Load(XmlReader, XsltSettings, XmlResolver)

编译包含在 . 中的 XmlReaderXSLT 样式表。 解析 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 比 XslTransform 慢?

注释

在调试模式下编译的 XSLT 与在发布模式下编译的 XSLT 之间存在差异。 在某些情况下,在调试模式下编译的样式表在调试期间 Load不会引发错误,但以后会在期间 Transform失败。 在发布模式下编译的同一样式表在发布期间 Load将失败。 此类行为的一个示例是,当不具有节点集类型的变量被分配给需要节点集的表达式时。

Load(IXPathNavigable, XsltSettings, XmlResolver)

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

编译包含在 . 中的 IXPathNavigableXSLT 样式表。 解析 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 中,它可以是(通常是)XmlNodeXmlDocumentXPathDocument包含样式表。

settings
XsltSettings

XsltSettings要应用于样式表。 如果是, nullDefault 应用该设置。

stylesheetResolver
XmlResolver

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

例外

值为 stylesheetnull.

样式表包含错误。

示例

以下示例加载样式表。 该 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
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要应用于样式表。 如果是, nullDefault 应用该设置。

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默认设置用于加载样式表。 DTD 处理在 .XmlReader 如果需要 DTD 处理,请创建启用了此功能的一个 XmlReader ,并将其传递给 Load 该方法。

另请参阅

适用于

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

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
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);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method will call into constructors of the earlyBoundTypes array which cannot be statically analyzed.")]
public void Load(System.Reflection.MethodInfo executeMethod, byte[] queryData, Type[]? earlyBoundTypes);
public void Load(System.Reflection.MethodInfo executeMethod, byte[] queryData, Type[]? earlyBoundTypes);
public void Load(System.Reflection.MethodInfo executeMethod, byte[] queryData, Type[] earlyBoundTypes);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method will call into constructors of the earlyBoundTypes array which cannot be statically analyzed.")>]
member this.Load : System.Reflection.MethodInfo * byte[] * Type[] -> unit
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 减少百分之十。

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
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

编译包含在 . 中的 XmlReaderXSLT 样式表。 解析 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要应用于样式表。 如果是, nullDefault 应用该设置。

stylesheetResolver
XmlResolver

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

例外

值为 stylesheetnull.

样式表包含错误。

示例

以下示例加载样式表并启用对 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
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 表。

例外

值为 stylesheetnull.

样式表包含错误。

示例

以下示例加载对象中包含的 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
Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs

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

public:
 void Load(Type ^ compiledStylesheet);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method will get fields and types from the assembly of the passed in compiledStylesheet and call their constructors which cannot be statically analyzed")]
public void Load(Type compiledStylesheet);
public void Load(Type compiledStylesheet);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method will get fields and types from the assembly of the passed in compiledStylesheet and call their constructors which cannot be statically analyzed")>]
member this.Load : Type -> unit
member this.Load : Type -> unit
Public Sub Load (compiledStylesheet As Type)

参数

compiledStylesheet
Type

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

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

属性

示例

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

注释

编译代码时,必须引用 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
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。

例外

值为 stylesheetUrinull.

样式表包含错误。

找不到样式表。

该值 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默认设置用于加载样式表。 DTD 处理在 .XmlReader 如果需要 DTD 处理,请创建启用了此功能的一个 XmlReader ,并将其传递给 Load 该方法。

  • 函数 document() 处于禁用状态。

  • 不支持嵌入的脚本。

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

另请参阅

适用于

Load(IXPathNavigable)

Source:
XslCompiledTransform.cs
Source:
XslCompiledTransform.cs
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 中,它可以是(通常是)XmlNodeXmlDocumentXPathDocument包含样式表。

例外

值为 stylesheetnull.

样式表包含错误。

示例

以下示例加载对象中包含的 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 设置。

另请参阅

适用于