您的位置:首页 > Web前端 > HTML

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

2007-10-31 21:30 597 查看
将XSL文件作为网页模板,数据存放在XML文件中。下的面XmlToHtml类将XML+XSL文件转化为HTML文件。

/********************************************************************
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());
}
}
}
}

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-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文件范例(Main.XML):
<?xml version="1.0" encoding="gb2312" ?>
<company>
<name>凌剑</name>
<address1>中国y</address1>
<address2>Some avenue</address2>
<city>深圳</city>
<country>中国</country>
</company>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: