C#中使用XSLT文件将XML文档转换为HTML
2011-05-12 16:58
871 查看
1.创建XSLT文件,定义格式;
2.在C#中使用XslCompiledTransform对象的load()方法加载XSLT文件;
3.使用Transform()方法转换XML文档。
实例:将books.xml按照books.xsl定义的格式转换成out.html
TestXsl.cs:
books.xml:
books.xsl:
out.html:
2.在C#中使用XslCompiledTransform对象的load()方法加载XSLT文件;
3.使用Transform()方法转换XML文档。
实例:将books.xml按照books.xsl定义的格式转换成out.html
TestXsl.cs:
using System; using System.Xml.Xsl; namespace Magci.Test.XML.TestXsl { class Program { static void Main(string[] args) { XslCompiledTransform trans = new XslCompiledTransform(); trans.Load(@"..\..\books.xsl"); trans.Transform(@"..\..\books.xml", "out.html"); } } }
books.xml:
<?xml version="1.0" encoding="utf-8" ?> <bookstore> <book genre="autobiography" publicationdate="1991" 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>
books.xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <head> <title>Price List</title> </head> <body> <table> <xsl:apply-templates/> </table> </body> </HTML> </xsl:template> <xsl:template match="bookstore"> <xsl:apply-templates select="book"/> </xsl:template> <xsl:template match="book"> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="price"/> </td> </tr> </xsl:template> </xsl:stylesheet>
out.html:
<HTML> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Price List</title> </head> <body> <table> <tr> <td>The Autobiography of Benjamin Franklin</td> <td>8.99</td> </tr> <tr> <td>The Confidence Man</td> <td>11.99</td> </tr> <tr> <td>The Gorgias</td> <td>9.99</td> </tr> </table> </body> </HTML>
相关文章推荐
- c#使用XSLT将xml文档转换为html文档
- c#使用XSLT将xml文档转换为html文档
- C#操作Xml:XSLT语法 在.net中使用XSLT转换xml文档示例
- C# 使用 wkhtmltopdf 把HTML文本或文件转换为PDF
- 使用XSLT将XML数据转换成HTML
- 在C#中用xslt文件格式化转换xml
- 使用XSLT将XML数据转换成HTML
- 使用XSLT将XML数据转换成HTML
- .net中使用XSLT转换xml文档
- 使用XSLT将XML数据转换成HTML
- 使用 JTidy 将 HTML 文件转换成 XML
- C#中如何将XML+XSL文档转换为HTML格式
- XSLT实现XML文档转换成HTML文档
- [唐胡璐] - C#将QTP结果(XML+XSL文档)转换为HTML格式
- C#使用xslt转换xml
- 使用XSLT将XML数据转换成HTML
- XSLT实现XML文档转换成HTML文档
- 解析在.net中使用XSLT转换xml文档的示例详解
- XML 文件:使用 JScript、C# 和 Visual Basic.NET 扩展 XSLT
- 使用XSLT将XML数据转换成HTML