XML 清单(CD.xml):
- <?xml version="1.0" encoding="utf-8" ?>
- <catalog>
- <cd>
- <title>Empire Burlesque</title>
- <artist>Bob Dylan</artist>
- <country>USA</country>
- <company>Columbia</company>
- <price>10.90</price>
- <year>1985</year>
- </cd>
- <cd>
- <title>Quxu</title>
- <artist>Genshui Yang</artist>
- <country>CHINA</country>
- <company>Chengshi</company>
- <price>12.90</price>
- <year>1987</year>
- </cd>
- </catalog>
XSL 清单(CD.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:template match="/">
- <html>
- <body>
- <table border="2" bgcolor="yellow">
- <tr>
- <th>Title</th>
- <th>Artist</th>
- </tr>
- <xsl:for-each select="catalog/cd">
- <tr>
- <td>
- <xsl:value-of select="title"/>
- </td>
- <td>
- <xsl:value-of select="artist"/>
- </td>
- </tr>
- </xsl:for-each>
- </table>
- </body>
- </html>
- </xsl:template>
- </xsl:stylesheet>
服务器端后台代码(C#, Default.aspx.cs):
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- using System.Xml;
- using System.Xml.Xsl;
- using System.Text;
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (Page.IsPostBack)
- return;
- XslCompiledTransform xslCT = new XslCompiledTransform();
- xslCT.Load(Server.MapPath("CD.xslt"));
- XmlDocument xmlDoc = new XmlDocument();
-
- StringBuilder strB=new StringBuilder();
- XmlWriter xw = XmlWriter.Create(strB);
- xslCT.Transform(Server.MapPath("CD.xml"), xw);
- Response.Write(strB.ToString());
-
- }
- }
Default.aspx 无需加任何代码.
运行Default.aspx页面, 显示结果如下:
Title |
Artist |
Empire Burlesque |
BobDylan |
Quxu |
Genshui Yang |
作者:Gezidan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted on 2011-08-15 09:53
日需博客 阅读(426)
评论(0) 编辑 收藏 引用 所属分类:
C# 、
Windows 、
技术文章 、
转载