摘要:RSS應用
範例RSS URL
http://www.epa.gov.tw/ch/RSS.aspx?type=1 (環保署最新消息)
########################## web網頁 #######################################3
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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 id="Head1" runat="server">
<title>RSStoGridview</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Url:"></asp:Label>
<asp:TextBox ID="txbUrl" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnRSS" runat="server" OnClick="btnRSS_Click" Text="RSS" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Vertical" ShowHeader="False">
<RowStyle BackColor="#F7F7DE" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("link") %>' Text='<%# Eval("title") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
############################ .cs 程式碼 #########################################
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.Data;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRSS_Click(object sender, EventArgs e)
{
string url = this.txbUrl.Text;
XmlDocument xmldoc = new XmlDocument();
//由Url載入RSS內容
xmldoc.Load(url);
//選擇內容節點
XmlNodeList nodes = xmldoc.SelectNodes("rss/channel/item");
DataSet ds = new DataSet();
//將選出來的內容加入DataSet
foreach (XmlNode node in nodes)
{
ds.ReadXml(new XmlNodeReader(node), XmlReadMode.Auto);
}
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
}
}