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>
新闻热点
疑难解答
图片精选