简体   繁体   中英

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.

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 .

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.

一个有点相关的帖子的好例子 - 将null传递给`XslCompiledTransform.Transform`方法

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM