一、通过XMlDAtaSource控件和DataList控件转换xml文件
写一个xml 文件,注意保存的路径,当前所示的是在同一个路径下的演示
<?xml version="1.0" encoding="utf-8" ?>
<Employees>
<Employee>
<name>东方</name>
<sex>男</sex>
<age>23</age>
<skill>ASP.NET</skill>
</Employee>
<Employee>
<name>小威</name>
<sex>男</sex>
<age>24</age>
<skill>JSP</skill>
</Employee>
<Employee>
<name>英子</name>
<sex>女</sex>
<age>22</age>
<skill>软件开发</skill>
</Employee>
<Employee>
<name>小魏</name>
<sex>女</sex>
<age>21</age>
<skill>网站制作</skill>
</Employee>
</Employees>
再写一个XSLT文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Employees">
<Employees>
<xsl:apply-templates select="Employee"/>
</Employees>
</xsl:template>
<xsl:template match="Employee">
<Employee>
<xsl:attribute name="name">
<xsl:value-of select="name"/>
</xsl:attribute>
<xsl:attribute name="sex">
<xsl:value-of select="sex"/>
</xsl:attribute>
<xsl:attribute name="age">
<xsl:value-of select="age"/>
</xsl:attribute>
<xsl:attribute name="skill">
<xsl:value-of select="skill"/>
</xsl:attribute>
</Employee>
</xsl:template>
</xsl:stylesheet>
写一个aspx页面用来装xml数据,拖一个XMlDAtaSource和DAtaList控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="xmlEemployee.aspx.cs" Inherits="xmlLeard.xmlEemployee" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>通过XMlDAtaSource控件和DataList控件转换</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/XMLEmployees.xml"
TransformFile="~/XSLTEmployees.xslt"></asp:XmlDataSource>
<asp:DataList ID="DataList1" runat="server" DataSourceID="XMlDataSource1" Caption="职员信息列表"
CellPadding="30" RepeatColumns="2">
<ItemTemplate>
姓名:<asp:Label ID="nameLabel" runat="server" Text='<%#Eval("name") %>' />
<br />
性别:<asp:Label ID="sexLabel" runat="server" Text='<%#Eval("sex") %>' />
<br />
年龄:<asp:Label ID="ageLabel" runat="server" Text='<%#Eval("age") %>' />
<br />
技术:<asp:Label ID="skillLabel" runat="server" Text='<%#Eval("skill") %>' />
<br />
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" />
</asp:DataList>
<br />
</div>
</form>
</body>
</html>
二、通过代码完成XMl转换
写一个xml文件
<?xml version="1.0" encoding="utf-8" ?>
<catalog>
<cd>
<title>学习雷锋好榜样</title>
<artist>匿名</artist>
<country>未知</country>
<company>正德科技</company>
<price>$50</price>
<year>1973.01.01</year>
</cd>
<cd>
<title>向前进</title>
<artist>匿名</artist>
<country>中华人民共和国</country>
<company>重庆正德科技股份有限公司</company>
<price>$20</price>
<year>1985.11.11</year>
</cd>
<cd>
<title>我爱中国</title>
<artist>匿名</artist>
<country>中国</country>
<company>正德科技</company>
<price>$10</price>
<year>2008.02.22</year>
</cd>
</catalog>
再写一个xslt文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2 align="center">专辑信息列表</h2>
<table border="1" align="center">
<tr bgcolor="#FB6704">
<th align="center">专辑名称</th>
<th align="center">艺术家</th>
<th align="center">国家/地区</th>
<th align="center">所属公司</th>
<th align="center">专辑价格</th>
<th align="center">出版日期</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td>
<xsl:value-of select="title"/>
</td>
<td>
<xsl:value-of select="artist"/>
</td>
<td>
<xsl:value-of select="country"/>
</td>
<td>
<xsl:value-of select="company"/>
</td>
<td>
<xsl:value-of select="price"/>
</td>
<td>
<xsl:value-of select="year"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
最后写一个aspx页面,在后台代码实现
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="xmlLeard._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>通过代码完成XMl转换</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;
namespace xmlLeard
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string XMLFile = Server.MapPath("~/XMLFile.xml");
string XSLTFile = Server.MapPath("~/XSLTFile.xslt");
// 代码解析xml文件
XmlTextReader xStylexmlreader = new XmlTextReader(XMLFile);
XPathDocument xStyledoc = new XPathDocument(xStylexmlreader,XmlSpace.Preserve);
xStylexmlreader.Close();
XPathNavigator xStyleNav = xStyledoc.CreateNavigator();
XmlTextReader xstyleread = new XmlTextReader(XSLTFile);
XslCompiledTransform xTan = new XslCompiledTransform();
xTan.Load(xstyleread);
xstyleread.Close();
//文件流读取
StringWriter sw = new StringWriter();
xTan.Transform(xStyleNav,null,sw);
Response.BufferOutput = true;
Response.Write(sw.ToString());
sw.Close();
}
}
}
三、调用XMl控件
写一个xml 文件
<?xml version="1.0" encoding="utf-8" ?>
<News>
<New>
<Id>001</Id>
<Title>
”去百度、Google一下“成网络流传笑话
</Title>
<From>新京报</From>
<Time>2008-5-23 1:38:00</Time>
<Content>网络流传着这样一个笑话”去百度、Google一下“。这句话虽是戏言,但也反映出这样的事实,正如商业周刊给google品牌的评价:
最新的牛津辞典中收录google为动词。这证实了google对手们的恐惧:对于大批的网络用户来说,google就是搜索的代名词。
</Content>
</New>
<New>
<Id>003</Id>
<Title>
哪些IT品牌在消费者中人缘最好?
</Title>
<From>新华网</From>
<Time>2008-4-1 17:18:00</Time>
<Content>
最红的明星、最眩的创意、最时尚的外表、最动听的广告语,每个IT品牌都在不惜重本的通过各种途径大张旗鼓的宣扬自己。
但产品究竟好不好并不由广告说了算,在IT产品极大丰富的今天,终端消费者的话语权越来越被重视。
</Content>
</New>
<New>
<Id>007</Id>
<Title>
中国IT卖场20年颐高10年庆祝活动全面启动
</Title>
<From>CCTV</From>
<Time>2008-4-1 17:45:34</Time>
<Content>
3月27日,在国内现存第一家IT专业卖场诞生地深圳,”纪念中国IT卖场20年大型系列活动“正式拉开序幕。
当天,中国IT连锁第一强颐高集团十周年庆祝活动也宣告启动。
</Content>
</New>
</News>
写一个xslt文件
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head>
<title>新闻信息列表</title>
</head>
<body>
<h3 align="center">最新IT新闻</h3>
<table border="1">
<tr>
<th>编号</th>
<th>标题</th>
<th>来源</th>
<th>时间</th>
<th>内容</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="New">
<tr>
<td width="40">
<xsl:value-of select="Id"/>
</td>
<td>
<xsl:value-of select="Title"/>
</td>
<td>
<xsl:value-of select="From"/>
</td>
<td>
<xsl:value-of select="Time"/>
</td>
<td>
<xsl:value-of select="Content"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
最后写一个aspx页面,拖一个xml控件,需要设置xml控件的DocumentSource和TransformSource这个两个属性
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="News.aspx.cs" Inherits="xmlLeard.News" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>用XML控件解析转换</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Xml ID="Xml1" runat="server" DocumentSource="~/XMLNews.xml" TransformSource="~/XSLTNews.xslt"></asp:Xml></div>
</form>
</body>
</html>