Export (0) Print
Expand All

XslTransform.Load Method (XmlReader)

Loads the XSLT style sheet contained in the XmlReader.

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

public void Load(
	XmlReader stylesheet
)

Parameters

stylesheet
Type: System.Xml.XmlReader

An XmlReader object that contains the XSLT style sheet.

ExceptionCondition
XsltCompileException

The current node does not conform to a valid style sheet.

SecurityException

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

NoteNote

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.

This method loads the XSLT style sheet, including any style sheets referenced in xsl:include and xsl:import elements. External resources are resolved using an XmlUrlResolver with no user credentials. If the style sheet(s) are located on a network resource which requires authentication, use the overload that takes an XmlResolver as one of its arguments and specify an XmlResolver with the necessary credentials.

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 returns, 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).

If the style sheet contains entities, you should specify an XmlReader that can resolve entities (XmlReader.CanResolveEntity returns true). In this case, an XmlValidatingReader can be used.

If the style sheet contains embedded scripting, the script is compiled to an assembly. The assembly has full trust. The recommended practice is to provide evidence using the Load(XmlReader, XmlResolver, Evidence) method. 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.

NoteNote

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 file sorting all the books by title.

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

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

  public static void Main()
  {
    //Create the reader to load the stylesheet.  
    //Move the reader to the xsl:stylesheet node.
    XmlTextReader reader = new XmlTextReader(stylesheet);
    reader.Read();
    reader.Read();

    //Create the XslTransform object and load the stylesheet.
    XslTransform xslt = new XslTransform();
    xslt.Load(reader);

    //Load the file to transform.
    XPathDocument doc = new XPathDocument(filename);

    //Create an XmlTextWriter which outputs to the console.
    XmlTextWriter writer = new XmlTextWriter(Console.Out);

    //Transform the file and send the output to the console.
    xslt.Transform(doc, null, writer);
    writer.Close();  

  }
}

The example uses the following data files as input.

books.xml

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

titles.xsl

<!--Stylesheet to sort all books by title-->
<!--Created 2/13/2001-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="bookstore">
      <books>
        <xsl:apply-templates select="book">
          <xsl:sort select="title"/>
        </xsl:apply-templates>
       </books>
   </xsl:template>
  <xsl:template match="book">
          <book><xsl:copy-of select="node()"/></book>
  </xsl:template>
</xsl:stylesheet>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.0
Obsolete (compiler warning) in 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft