本文的发布号曾为 CHS307494
概要
本文介绍了如何通过使用 XslTransform 类将可扩展样式表语言 (XSL) 转换 (XSLT) 语言应用于可扩展标记语言 (XML) 文档来创建新的 XML 文档。XSL 是一种基于 XML 的语言,它旨在将一个 XML 文档转换为另一个 XML 文档或将 XML 文档转换为任何其他结构化文档。返回页首
要求
下面的列表列出了推荐使用的硬件、软件、网络结构以及所需的服务包:- Microsoft Visual Studio .NET
- Microsoft Visual Studio .NET 软件开发工具包 (SDK) 快速入门
- XML 技术
- 创建和读取 XML 文件
- XML 路径语言 (XPath) 语法
- XSL
如何应用 XSL 转换
本示例使用了名为 Books.xml 和 Books.xsl 的两个文件。您可以使用自己的 Books.xml 和 Books.xsl 文件或使用 .NET 软件开发工具包 (SDK) 快速入门中包含的示例文件。必须将 Books.xml 和 Books.xsl 文件复制到 /Bin/Debug 文件夹中,该文件夹位于创建此项目所在的文件夹的下面。可以在以下文件夹中找到这些文件:../Program Files/Microsoft Visual Studio .NET/FrameworkSDK/Samples/QuickStart/Howto/Samples/Xml/Transformxml/Cs
- 在 Visual C# .NET 中新建一个控制台应用程序。
- 确保该项目包含一个对 System.Xml 命名空间的引用;如果不包含,则请添加此引用。
- 对 Xml、XPath 和 Xsl 命名空间使用 using 语句,这样,在后面的代码中就不需要限定这些命名空间中的声明了。using 语句必须位于任何其他声明之前。
using System.Xml; using System.Xml.Xsl; using System.Xml.XPath;
- 声明相应的变量。声明一个 XPathDocument 对象以保存 XML 文档,声明一个 XslTransform 对象以转换 XML 文档。在 Module1 的 Main 过程中添加声明代码:
XslTransform myXslTransform; XPathDocument myXPathDocument;
- 用 Books.xml 示例文件填充 XPathDocument 对象。XPathDocument 类提供一种快速和面向性能的缓存来使用 XSLT 处理 XML 文档。XPathDocument 类与 XML 文档对象模型 (DOM) 类似,但前者针对 XSLT 处理和 XPath 数据模型进行了高度优化。
myXPathDocument = new XPathDocument ("books.xml");
- 构建一个新的 XslTransform 对象。XslTransform 类是一种用于实现 XSL 转换 (XSLT) 1.0 推荐代码的 XSLT 处理器:
myXslTransform = new XslTransform();
- 使用 Load 方法加载带有此样式表的 XslTransform 对象。这个样式表将 Books.xsl 文档的详细信息转换为书籍的简明国际标准图书编号 (ISBN) 列表:
myXslTransform.Load("books.xsl");
- 用转换后的新 XML 文件名创建一个 XmlTextWriter 类。调用 Transform 方法以开始转换。
XmlTextWriter writer = new XmlTextWriter("ISBNBooks.xml",System.Text.Encoding.UTF8); myXslTransform.Transform(myXPathDocument,null, writer); writer.Flush(); writer.Close();
- 或者,您也可以将转换成的 XML 文档发送到 XmlReader、Stream、或 TextWriter 类。下面的代码示例将 XML 转换发送到 StringWriter(TextWriter 的派生)的实例中,此实例转而将该转换写入控制台窗口。
System.IO.StringWriter stWrite = new System.IO.StringWriter(); myXslTransform.Transform(myXPathDocument, null, stWrite); Console.WriteLine(stWrite.ToString); Console.ReadLine();
备注: 完整代码列表使用上述代码而不使用第 8 步中的代码。 - 生成并运行您的项目。 转换的结果显示在控制台窗口中,如下所示:
<root><bookstore><book ISBN="1-861003-11-0"><price>8.99</price> </book><book ISBN="0-201-63361-2"><price>11.99</price> </book><book ISBN="1-861001-57-6"><price>9.99</price> </book></bookstore></root>
完整代码示例
using System; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; using System.IO; namespace XSLTransformfromXPath { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { static void Main(string[] args) { XslTransform myXslTransform; XPathDocument myXPathDocument = new XPathDocument ("books.xml"); myXslTransform = new XslTransform(); myXslTransform.Load("books.xsl"); XmlTextWriter writer = new XmlTextWriter("ISBNBooks.xml",System.Text.Encoding.UTF8); myXslTransform.Transform(myXPathDocument,null, writer); writer.Flush(); writer.Close(); System.IO.StringWriter stWrite = new System.IO.StringWriter(); myXslTransform.Transform(myXPathDocument, null, stWrite); Console.WriteLine(stWrite.ToString()); Console.ReadLine(); } } }返回页首
参考
有关 XmlReader 类的更多信息,请参阅以下 Microsoft .NET 框架类库文档:http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemxmlxslxsltransformclasstopic.htm
有关包含 XslTransform 对象的 XslTransform 类的信息,请参阅以下 Microsoft .NET 框架开发指南文档: 有关 XSLT 和 Active Server Pages .NET 的实际比较,请参阅以下 MSDN 文章:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnexxml/html/xml02192001.asp
有关 .NET 中的 XML 的更多信息,请到以下 Microsoft Web 站点查阅 MSDN Magazine 中的“.NET Framework XML Classes and C# Offer Simple, Scalable Data Manipulation”(.NET 框架 XML 类和 C# 提供简单的可缩放数据操作)一文:http://msdn.microsoft.com/msdnmag/issues/01/01/xml/xml.asp
返回页首这篇文章中的信息适用于:
- Microsoft Visual C# .NET (2002)
最近更新: | 2002-2-24 (1.0) |
关键字 | kbhowto kbHOWTOmaster KB307494 |