C#将XML+XSL文件转化为HTML文件的类
2007-10-31 21:30
597 查看
将XSL文件作为网页模板,数据存放在XML文件中。下的面XmlToHtml类将XML+XSL文件转化为HTML文件。
XSL文件范例(Main.XSL):
XML文件范例(Main.XML):
/******************************************************************** created: 2007/10/31 created: 31:10:2007 20:09 filename: D:/C#程序练习/XmlToHtml/XmlToHtml.cs file path: D:/C#程序练习/XmlToHtml file base: XmlToHtml file ext: cs author: 凌剑Bujiwu purpose: XML+XSL文件转化为HTML文件 *********************************************************************/ using System; using System.IO; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; namespace Xmltohtml { class XmlToHtml { private string XslFilePath; //初始化,传入XSL文件路径 public XmlToHtml(string XslFilePath) { this.XslFilePath = XslFilePath; } //将XmlFileDir目录所有的XML创建相应的HTML文件 public void CreateHtmlFile(string XmlFileDir) { DealWithXmlFile(XmlFileDir); } //根据单个XML文件创建HTML文件 public void CreateSigleHtmlFile(string XmlFilePath) { XmlTransToHtml(XmlFilePath); } //搜索XmlFilePath在的XML文件 private void DealWithXmlFile(string XmlFileDir) { //创建数组保存源文件夹下的文件名 string[] strFiles = Directory.GetFiles(XmlFileDir, "*.xml"); for (int i = 0; i < strFiles.Length; i++) { XmlTransToHtml(strFiles[i]); } DirectoryInfo dirInfo = new DirectoryInfo(XmlFileDir); //取得源文件夹下的所有子文件夹名称 DirectoryInfo[] ZiPath = dirInfo.GetDirectories(); for (int j = 0; j < ZiPath.Length; j++) { //获取所有子文件夹名 string strZiPath = XmlFileDir + "//" + ZiPath[j].ToString(); //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索 DealWithXmlFile(strZiPath); } } //将sXmlPath的XML文件转化为Html文件 private void XmlTransToHtml(string sXmlPath) { try { //生成Html文件路径 string HtmlFilePath = sXmlPath.Substring(0, sXmlPath.Length - 3) + "html"; XPathDocument myXPathDoc = new XPathDocument(sXmlPath); XslCompiledTransform myXslTrans = new XslCompiledTransform(); //加载XSL文件 myXslTrans.Load(XslFilePath); XmlTextWriter myWriter = new XmlTextWriter(HtmlFilePath, System.Text.Encoding.Default); myXslTrans.Transform(myXPathDoc, null, myWriter); myWriter.Close(); } catch (Exception e) { Console.WriteLine("Exception: {0}", e.ToString()); } } } } |
<!-- - XSLT is a template based language to transform Xml documents It uses XPath to select specific nodes for processing. - A XSLT file is a well formed Xml document --> <!-- every StyleSheet starts with this tag --> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <!-- indicates what our output type is going to be --> <xsl:output method="html" /> <!-- Main template to kick off processing our Sample.xml From here on we use a simple XPath selection query to get to our data. --> <xsl:template match="/"> <html> <head> <title>Welcome to <xsl:value-of select="/company/name"/></title> <style> body,td {font-family:Tahoma,Arial; font-size:9pt;} </style> </head> <body> <h2>Welcome to <xsl:value-of select="/company/name"/></h2> <p/> <b>Our contact details:</b> <br/> <br/> <xsl:value-of select="/company/name"/> <br/> <xsl:value-of select="/company/address1"/> <br/> <xsl:value-of select="/company/address2"/> <br/> <xsl:value-of select="/company/city"/> <br/> <xsl:value-of select="/company/country"/> </body> </html> </xsl:template> </xsl:stylesheet> |
<?xml version="1.0" encoding="gb2312" ?> <company> <name>凌剑</name> <address1>中国y</address1> <address2>Some avenue</address2> <city>深圳</city> <country>中国</country> </company> |
相关文章推荐
- C#将XML+XSL文件转化为HTML文件的类
- C# 将xml中二进制文件 转化为视频 以及 根据节点读取节点值
- 将xml文件按照xsl文件样式快速生成html文件的方法
- C#实现XML文件读写
- c#代码查询带命名空间的xml文件
- c#将xml字符串转化为dataset
- 使用C++把文件夹目录信息转化为XML文件
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 将xml文件转换成可以浏览的html文件
- C# Execl表格文件转xml文件
- C#中XML文档生成实例:Elevator Scheduler测试文件的生成详解
- JAVA将list转化为xml文件
- C# XML配置文件读写类(用于程序配置保存)
- C# DataSet,ListView和XML文件互操作的例子
- Java对象与XML文件之间相互转化(含源代码)
- C#使用XmlEDocument实现数据文件安全存储功能
- XML文件转化成NSData对象的方法
- c#操作xml文件
- C# XML 文档注释文件格式
- C#读取XML文件经典案例