XslTransform.Load Method (String, XmlResolver)


Loads the XSLT style sheet specified by a URL.

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

public void Load(
	string url,
	XmlResolver resolver


Type: System.String

The URL that specifies the XSLT style sheet to load.

Type: System.Xml.XmlResolver

The XmlResolver to use to load the style sheet and any style sheet(s) referenced in xsl:import and xsl:include elements.

If this is null, a default XmlUrlResolver with no user credentials is used to open the style sheet. The default XmlUrlResolver is not used to resolve any external resources in the style sheet, so xsl:import and xsl:include elements are not resolved.

The XmlResolver is not cached after the Load method completes.

Exception Condition

The loaded resource is not a valid style sheet.


The style sheet contains embedded script, and the caller does not have UnmanagedCode permission.


The XslTransform class is obsolete in the .NET Framework version 2.0. The XslCompiledTransform class is the new XSLT processor. For more information, see Using the XslCompiledTransform Class and Migrating From the XslTransform Class.

XslTransform supports the XSLT 1.0 syntax. The XSLT style sheet must include the namespace declaration xmlns:xsl= http://www.w3.org/1999/XSL/Transform.

If the style sheet contains embedded scripting, the script is compiled to an assembly. The URI of the style sheet is used to create evidence, which is applied to the assembly. For issues with the Load method and style sheets with embedded scripts, see article Q316755 in the Microsoft Knowledge Base at http://support.microsoft.com.


If the caller does not have UnmanagedCode permission, the embedded script is not compiled and a SecurityException is thrown. See SecurityPermission and SecurityPermissionFlag.UnmanagedCode for more information.

The following example transforms an XML document into an HTML document. The example loads an XSLT style sheet which contains an xsl:include element referencing another style sheet. An XmlUrlResolver is passed to the Load method which sets the credentials necessary to access the network resource for the included style sheet.

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.Net;

public class Sample
   private const String filename = "books.xml";
   private const String stylesheet = "sort.xsl";

   public static void Main()
      //Create the XslTransform.
     XslTransform xslt = new XslTransform();

     //Create a resolver and set the credentials to use.
     XmlUrlResolver resolver = new XmlUrlResolver();
     resolver.Credentials = CredentialCache.DefaultCredentials;

     //Load the stylesheet.
     xslt.Load(stylesheet, resolver);

     //Load the XML data file.
     XPathDocument doc = new XPathDocument(filename);

     //Create the XmlTextWriter to output to the console.            
     XmlTextWriter writer = new XmlTextWriter(Console.Out);

     //Transform the file.
     xslt.Transform(doc, null, writer, null);

The example uses the following data files as input.


<!-- This file represents a fragment of a book store inventory database -->
  <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
  <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
  <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
    <title>The Gorgias</title>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="bookstore"/>
  <xsl:include href="http://serverA/includefile.xsl"/>  
  <xsl:template match="book">
      <TD><xsl:value-of select="@ISBN"/></TD>
      <TD><xsl:value-of select="title"/></TD>
      <TD><xsl:value-of select="price"/></TD>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="bookstore">
    <TABLE BORDER="2">
    <xsl:apply-templates select="book">
      <xsl:sort select="@ISBN"/>

.NET Framework
Available since 1.1
Return to top