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>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章