XSLT与.NET
发布时间:2020-12-17 01:08:42 所属栏目:安全 来源:网络整理
导读:?在Net中主要使用XslCompiledTransform类进行编译样式表并执行XSLT 转换. 在下面示例以一个WebService进行示例 using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Services;using System.Data;using Syst
?在Net中主要使用XslCompiledTransform类进行编译样式表并执行XSLT 转换. 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; } } }
<?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') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |