微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XSLT与.NET

 在Net中主要使用xslcompiledtransform类进行编译样式表并执行XSLT 转换.
在下面示例以一个WebService进行示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.sqlClient;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
using System.Text;

namespace xslt
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolBoxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class Products : System.Web.Services.WebService
    {
        [WebMethod]
        public string GetProduct()
        {
            sqlConnection conn = new sqlConnection(getConnectionString());
            sqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "usp_GetProductsXmlXPath";
            conn.open();
            XmlReader reader = cmd.ExecuteXmlReader();
            reader.Read();
            string s = reader.ReadOuterXml();
            reader.Close();
            conn.Close();
            //transform
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(s);
            //xslt进行转换xml
            xslcompiledtransform ct = new xslcompiledtransform();           
            ct.Load(Server.MapPath("~/ProductXmlXPath.xslt"));
            MemoryStream ms = new MemoryStream();
            ct.Transform(doc,null,ms);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        public static string getConnectionString()
        {
            string ConnectionString;
            ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["northwindConnectionString"].ToString();
            return ConnectionString;
        }
    }
}


Xslt文件如下:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>
    <xsl:template match="/">
        <table style="background-color:lightgreen">
          <xsl:for-each select="./Products/Category">
          <tr>
            <td style="border:solid 1px blank;">
             <xsl:apply-templates select="ProductName"/>
            </td>
            <td style="border:solid 1px blank;">
              <xsl:apply-templates select="UnitPrice"/>
            </td>
          </tr>
          </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>
usp_GetProductsXmlXPath过程
CREATE PROCEDURE [dbo].[usp_GetProductsXmlXPath]
AS
  
  SELECT [ProductID],[ProductName],[supplierID],[CategoryID],[QuantityPerUnit],[UnitPrice]
  FROM [Products] 
  FOR XML Path('Category'),ROOT('Products') 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐