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

本文介绍了一个使用C#实现的XML到HTML转换程序示例。该程序通过XSLT转换技术,将XML数据按照指定样式表呈现为网页内容。文中详细展示了如何遍历文件夹结构并处理其中的所有XML文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

               
         将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);

            }

        }

 

        //sXmlPathXML文件转化为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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值