涉及XML的控件
一、xmldatasource:
功能:使 XML 数据可用于数据绑定控件。显示分层XML 数据;
属性:DataFile、TransformFile、Xpath;
<asp:XmlDataSource ID="MySource" DataFile="~/App_Data/Bookstore.xml"
XPath="bookstore/genre[@name='Fiction']/book" runat="server">
</asp:XmlDataSource>
1、使用 DataFile 属性指定XML 文件加载 XML 数据。
2、该控件将 XML 元素的属性公开为可绑定数据的字段。
3、使用Xpath筛选数据。
还可以使用Data属性内嵌XML数据:
<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
<DataBindings>
<asp:TreeNodeBinding ImageUrl="~/images/openbook.gif" TextField="Title"
DataMember="book" />
<asp:TreeNodeBinding ImageUrl="~/images/notepad.gif" TextField="name"
DataMember="chapter" />
</DataBindings>
</asp:TreeView>
</div>
<div>
<asp:XmlDataSource ID="XmlDataSource1" Runat="server" XPath="bookstore/book">
<Data>
<bookstore>
<book ISBN="10-861003-324" Title="A Tale of Two Cities" Price="19.99">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
<book ISBN="1-861001-57-5" Title="Pride And Prejudice" Price="24.95">
<chapter num="1" name="Introduction">
Abstract...
</chapter>
<chapter num="2" name="Body">
Abstract...
</chapter>
<chapter num="3" name="Conclusion">
Abstract...
</chapter>
</book>
</bookstore>
</Data>
</asp:XmlDataSource>
二、asp:XML:用于显示 XML 文档或 XSL 转换的结果,
| 属性 | 描述 | .NET |
|---|---|---|
| Document | 不赞成使用。规定使用 System.Xml.XmlDocument 对象的 XML 文档。 | 1.0 |
| DocumentContent | 规定 XML 字符串。 | 1.0 |
| DocumentSource | 规定在 Xml 控件中显示的 XML 文档的路径。 | 1.0 |
| runat | 规定该控件是服务器控件。必须设置为 "server"。 | 1.0 |
| Transform | 使用 System.Xml.Xsl.XslTransform 对象来格式化 XML 文档。 | 1.0 |
| TransformArgumentList | 包含传递给样式表并在扩展样式表语言转换 (XSLT) 中使用的可选参数列表。 | |
| TransformSource | 规定 XSL 转换文件的路径。 | |
| XPathNavigator | 用于导航和编辑与 Xml 控件关联的 XML 数据的光标模型。 |
通过设置 TransformSource 属性,可以选择指定 XSL 转换 (XSLT) 样式表,该样式表在 XML 文档被写入输出流之前对其进行格式化。这两个属性表示可用于格式化 XML 文档的不同类型的 XSL 转换样式表:
实现订阅实例:
客户端:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ProgXmlServerlTransform.aspx.cs" Inherits="ProgXmlServerlTransform" %>
<!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>Programmatically Displaying XML Data using Xml Web Server Control</title>
</head>
<body style="font-family:Arial, helvetica, sans-serif; font-size:12pt;
background-color:#EEEEEE">
<form id="form1" runat="server">
<div>
<asp:Button Text="订阅" OnClick="btn_Click" runat="server" ID="btn" />
<asp:Xml id="Xml1" runat="server" >
</asp:Xml>
</div>
</form>
</body>
</html>
服务器端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
public partial class ProgXmlServerlTransform : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load("http://www.cnblogs.com/rss");
Xml1.Document = doc;
Xml1.TransformSource = "~/App_Data/RSS.xsl";
}
}
Rss.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="rss/channel" />
</xsl:template>
<xsl:template match="channel">
<h2>
<a href="{link}" target="_blank"><xsl:value-of select="title" /></a>
</h2>
<ul>
<xsl:apply-templates select="item" />
</ul>
</xsl:template>
<xsl:template match="item">
<li>
<a href="{link}" target="_blank"><xsl:value-of select="title" /></a>
- <xsl:value-of select="pubDate" />
<br />
<xsl:value-of disable-output-escaping="yes" select="description" />
<p/>
</li>
</xsl:template>
</xsl:stylesheet>
三、asp:SiteMapDataSource:用于页面的导航:
1、编写一个叫web.sitemap的XML文本文件, 在该文件中定义出整个要导航页面的结构层次;
2、 向页面中添加sitemapdatasource控件。该控件会自动感应绑定web.sitemap中的内容;
3、 将sitemapdatasource控件绑定到如sitemappath,treeview,menu等控件中;
四、有关客户端XML控件详见《DSO》;
五、其他服务器端控件:
1、模板控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList_XML_Nested.aspx.cs" Inherits="DataList_XML_Nested" %>
<!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>Displaying XML Data in Nested DataList Controls</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Bookstore:Fiction</h1>
<asp:XmlDataSource ID="MySource" DataFile="~/App_Data/Bookstore.xml"
XPath="bookstore/genre[@name='Fiction']/book" runat="server">
</asp:XmlDataSource>
<asp:DataList ID="List1" DataSourceID="MySource" runat="server">
<ItemTemplate>
<table>
<tr>
<td>
<img alt="" src='<%#"images/"+XPath("@ISBN")+".jpg" %>' />
</td>
<td>
<h4><%# this.XPath("@Title") %></h4>
<b>ISBN:</b> <%# XPath("@ISBN") %><br>
<b>Price:</b> <%# XPath("@Price") %><br>
</td>
</tr>
</table>
<asp:DataList id="DataList2" DataSource='<%# XPathSelect("chapter") %>' runat="server">
<ItemTemplate>
<br>
<u>
Chapter <%# XPath("@num") %>:
<%# XPath("@name") %>
</u>
<br>
<%# XPath(".") %>
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>
2、非模板控件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridView_XML.aspx.cs" Inherits="GridView_XML" %>
<!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>Displaying XML Data in a GridView and a ListBox</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="lstTitles" runat="server" DataSourceID="XmlDataSource1"
DataValueField="ISBN" DataTextField="Title"></asp:ListBox>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="XmlDataSource1"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField HeaderText="ISBN" DataField="ISBN" />
<asp:BoundField HeaderText="Title" DataField="Title" />
<asp:BoundField HeaderText="Price" DataField="Price" />
</Columns>
</asp:GridView>
</div>
<div>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Bookstore.xml"
XPath="bookstore/genre[@name='Fiction']/book"></asp:XmlDataSource>
</div>
</form>
</body>
</html>

浙公网安备 33010602011771号