ASP.Net 2.0提供了多種數據源,一般情況下,XmlDataSource控件使用XML文件,下面的例子就是使用XMLDocument對象進行數據綁定的一種方法。
VB.Net代碼
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim doc As New System.Xml.XMLDocument
doc.Load("http://dotnet.aspx.cc/Rss.ASPx")
XmlDataSource1.Data = doc.InnerXML
XMLDataSource1.XPath = "/rss/channel/item"
End Sub
</script>
<html XMLns="http://www.w3.org/1999/xHtml">
<body>
<form id="form1" runat="server">
<ASP:GridView ID="GridVIEw1" runat="server" DataSourceID="XMLDataSource1" AutoGenerateColumns="false">
<Columns>
<ASP:TemplateFIEld HeaderText="文章標題">
<ItemTemplate>
<ASP:HyperLink runat="server" Target="_blank" NavigateUrl='<%#XPath("link") %>'>
<%#XPath("title")%></ASP:HyperLink>
[<%#CType(XPath("pubDate"), DateTime).ToString("yyyy年M月d日")%>]
</ItemTemplate>
</ASP:TemplateFIEld>
</Columns>
</ASP:GridVIEw>
<asp:XmlDataSource ID="XMLDataSource1" runat="server"></ASP:XMLDataSource>
</form>
</body>
</Html>
C#代碼
<%@ Page Language="C#" AutoEventWireup="true"%>
<script runat="server">
protected void Page_Load( object sender, System.EventArgs e )
{
System.Xml.XmlDocument doc = new System.Xml.XMLDocument();
doc.Load("http://dotnet.aspx.cc/Rss.ASPx");
XmlDataSource1.Data = doc.InnerXML;
XMLDataSource1.XPath = "/rss/channel/item";
}
</script>
<html XMLns="http://www.w3.org/1999/xHtml">
<body>
<form id="form1" runat="server">
<ASP:GridView ID="GridVIEw1" runat="server" DataSourceID="XMLDataSource1" AutoGenerateColumns="false">
<Columns>
<ASP:TemplateFIEld HeaderText="文章標題">
<ItemTemplate>
<ASP:HyperLink ID="HyperLink1" runat="server" Target="_blank" NavigateUrl='<%#XPath("link") %>'>
<%#XPath("title")%></ASP:HyperLink>
[<%#(DateTime.Parse(XPath("pubDate").ToString().Replace("GMT",""))).ToString("yyyy年M月d日")%>]
</ItemTemplate>
</ASP:TemplateFIEld>
</Columns>
</ASP:GridVIEw>
<asp:XmlDataSource ID="XMLDataSource1" runat="server"></ASP:XMLDataSource>
</form>
</body>
</Html>