XML 的 XSL 转换 - 简单的 .NET 示例?

问题描述:

我有一个基于 .NET 的应用程序,它接收传入的 XML 文件.我想使用我拥有的 XSL 样式表将 XML 文件转换为 HTML.这是我的过程...

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. 从文件系统读取提交的 XML 文件
  2. 将 XSL 应用到 XML 以进行转换
  3. 将结果 HTML 打印为 HTML 屏幕

有没有人有任何示例代码来演示如何做到这一点?谢谢.

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

这是 MSDN .NET 文档 关于使用 Transform() 方法 XslCompiledTransform 类,它是 .NET 的标准部分(在 System.Xml.Xsl 命名空间):

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);
}

剩下要做的是调用浏览器并将包含在流 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.

您应该熟悉Transform() *方法并选择最适合您的方法.