- 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());
- }
- }
- }
- }
XSL文件範例(Main.XSL):
- <!--
- - 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-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文件範例(Main.XML):
- <?xml version="1.0" encoding="gb2312" ?>
- <company>
- <name>凌劍</name>
- <address1>中國y</address1>
- <address2>Some avenue</address2>
- <city>深圳</city>
- <country>中國</country>
- </company>