webworm
webworm

Reputation: 11019

XSL Transformation of XML - Simple .NET example?

I have a.NET based application that receives an incoming XML file. I would like to transform the XML file into HTML using an XSL stylesheet I have. This is my process ...

  1. Read the submitted XML file from filesystem
  2. Apply XSL to XML for transformation
  3. Print resultant HTML to screen as HTML

Does anyone have any example code that demonstrates how to to this? Thank you.

Upvotes: 3

Views: 4202

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Here is a very short example from the MSDN .NET documentation on using the Transform() method of the XslCompiledTransform class that is a standard part of .NET (implemented in the System.Xml.Xsl namespace):

// Load the style sheet.
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("output.xsl");

// Create the FileStream.
using (FileStream fs = new FileStream(@"c:\data\output.xml", FileMode.Create))
{
   // Execute the transformation.
   xslt.Transform(new XPathDocument("books.xml"), null, fs);
}

What remains to be done is to invoke the browser and pass the result of the transformation, contained in the stream fs to it. If efficiency is important, one can choose to use memory stream over file stream.

You should get acquainted with the other overloads of the Transform() *method and choose the one that is best for you.

Upvotes: 6

Michael Kay
Michael Kay

Reputation: 163322

You haven't actually said which XSLT processor you are using. There are at least three available: the Microsoft one, which only supports XSLT 1.0, and Saxon and XQSharp which both support XSLT 2.0. All unfortunately have different APIs.

Upvotes: 1

webworm
webworm

Reputation: 11019

A good example from a somewhat related post - Passing null to `XslCompiledTransform.Transform` method

Upvotes: 0

Related Questions