C#将XML+XSL文件转化为HTML文件的类

  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Xsl;
  5. using System.Xml.XPath;
  6.  
  7. namespace Xmltohtml
  8. {
  9.     class XmlToHtml
  10.     {
  11.         private string XslFilePath;
  12.         //初始化,传入XSL文件路径
  13.         public XmlToHtml(string XslFilePath)
  14.         {
  15.             this.XslFilePath = XslFilePath;
  16.         }
  17.  
  18.         //将XmlFileDir目录所有的XML创建相应的HTML文件
  19.         public void CreateHtmlFile(string XmlFileDir)
  20.         {
  21.             DealWithXmlFile(XmlFileDir);
  22.         }
  23.  
  24.         //根据单个XML文件创建HTML文件
  25.         public void CreateSigleHtmlFile(string XmlFilePath)
  26.         {
  27.             XmlTransToHtml(XmlFilePath);
  28.         }
  29.  
  30.         //搜索XmlFilePath在的XML文件
  31.         private void DealWithXmlFile(string XmlFileDir)
  32.         {
  33.             //创建数组保存源文件夹下的文件名
  34.             string[] strFiles = Directory.GetFiles(XmlFileDir, "*.xml");
  35.             for (int i = 0; i < strFiles.Length; i++)
  36.             {
  37.                 XmlTransToHtml(strFiles[i]);
  38.             }
  39.  
  40.             DirectoryInfo dirInfo = new DirectoryInfo(XmlFileDir);
  41.             //取得源文件夹下的所有子文件夹名称
  42.             DirectoryInfo[] ZiPath = dirInfo.GetDirectories();
  43.             for (int j = 0; j < ZiPath.Length; j++)
  44.             {
  45.                 //获取所有子文件夹名
  46.                 string strZiPath = XmlFileDir + "//" + ZiPath[j].ToString();
  47.                 //把得到的子文件夹当成新的源文件夹,从头开始新一轮的搜索
  48.                 DealWithXmlFile(strZiPath);
  49.             }
  50.         }
  51.  
  52.         //将sXmlPath的XML文件转化为Html文件
  53.         private void XmlTransToHtml(string sXmlPath)
  54.         {
  55.             try
  56.             {
  57.                 //生成Html文件路径
  58.                 string HtmlFilePath = sXmlPath.Substring(0, sXmlPath.Length - 3) + "html";
  59.                 XPathDocument myXPathDoc = new XPathDocument(sXmlPath);
  60.                 XslCompiledTransform myXslTrans = new XslCompiledTransform();
  61.                 //加载XSL文件
  62.                 myXslTrans.Load(XslFilePath);
  63.  
  64.                 XmlTextWriter myWriter = new XmlTextWriter(HtmlFilePath, System.Text.Encoding.Default);
  65.                 myXslTrans.Transform(myXPathDoc, null, myWriter);
  66.                 myWriter.Close();
  67.             }
  68.             catch (Exception e)
  69.             {
  70.                 Console.WriteLine("Exception: {0}", e.ToString());
  71.             }
  72.         }
  73.     }
  74. }

XSL文件范例(Main.XSL):

  1. <!--
  2.        - XSLT is a template based language to transform Xml documents
  3.        It uses XPath to select specific nodes
  4.        for processing.
  5.       
  6.        - A XSLT file is a well formed Xml document
  7. -->
  8.  
  9. <!-- every StyleSheet starts with this tag -->
  10. <xsl:stylesheet
  11.       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  12.       version="1.0">
  13.  
  14. <!-- indicates what our output type is going to be -->
  15. <xsl:output method="html" />             
  16.       
  17.        <!--
  18.               Main template to kick off processing our Sample.xml
  19.               From here on we use a simple XPath selection query to
  20.               get to our data.
  21.        -->
  22.        <xsl:template match="/">
  23.       
  24.               <html>
  25.       
  26.                      <head>
  27.                            
  28.                             <title>Welcome to <xsl:value-of select="/company/name"/></title>
  29.                            
  30.                             <style>
  31.                                    body,td { font-size:9pt;}
  32.                             </style>
  33.                            
  34.                      </head>
  35.                     
  36.                      <body>
  37.                             <h2>Welcome to <xsl:value-of select="/company/name"/></h2>
  38.                             <p/>
  39.                             <b>Our contact details:</b>
  40.                             <br/>
  41.                             <br/>            
  42.                             <xsl:value-of select="/company/name"/>
  43.                             <br/>
  44.                             <xsl:value-of select="/company/address1"/>
  45.                             <br/>
  46.                             <xsl:value-of select="/company/address2"/>
  47.                             <br/>
  48.                             <xsl:value-of select="/company/city"/>
  49.                             <br/>
  50.                             <xsl:value-of select="/company/country"/>
  51.                      </body>
  52.       
  53.               </html>
  54.       
  55.        </xsl:template>
  56.  
  57. </xsl:stylesheet>

XML文件范例(Main.XML):


  1. <?xml version="1.0" encoding="gb2312" ?>
  2. <company>
  3.        <name>凌剑</name>
  4.        <address1>中国y</address1>
  5.        <address2>Some avenue</address2>
  6.        <city>深圳</city>
  7.        <country>中国</country>
  8. </company>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值