<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test01.aspx.cs" Inherits="Test01" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void ProcessRSSItem(string rssURL)
{
//使用rssURL的值建立了一個WebRequest項
System.Net.WebRequest myRequest = System.Net.WebRequest.Create(rssURL);
//WebRequest請求的響應將會被放到一個WebResponse對象myResponse裡,然後這個WebResponse對象被用來建立一個流來取出XML的值
System.Net.WebResponse myResponse = myRequest.GetResponse();
System.IO.Stream rssStream = myResponse.GetResponseStream();
//使用一個XmlDocument對象rssDoc來儲存流中的XML內容。XmlDocument對象用來調入XML的內容
System.Xml.XmlDocument rssDoc = new System.Xml.XmlDocument();
rssDoc.Load(rssStream);
//使用XPath表達,一個項節點列表可以如下方式建立
//rssItems儲存了從RSS裡獲得所有項節點的資訊
System.Xml.XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item");
string title = "";
string link = "";
string description = "";
for (int i = 0; i < rssItems.Count; i++)
{
//在 rssItems中儲存的每個項,每個標記(tag)元素都可以用SelectSingleNode方法提取出來。返回的值將被賦給一個XMLNode對象。
System.Xml.XmlNode rssDetail;
rssDetail = rssItems.Item(i).SelectSingleNode("title");
if (rssDetail != null)
{
title = rssDetail.InnerText;
}
else
{
title = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("link");
if (rssDetail != null)
{
link = rssDetail.InnerText;
}
else
{
link = "";
}
rssDetail = rssItems.Item(i).SelectSingleNode("description");
if (rssDetail != null)
{
description = rssDetail.InnerText;
}
else
{
description = "";
}
Response.Write("<p><b><a href='" + link + "' target='_blank'>" + title + "</a></b><br/>");
Response.Write(description + "</p>");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>讀取RSS</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
string rssURL = "http://www.search.hc360.com/xml/rss_r001068002002.xml";
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />");
ProcessRSSItem(rssURL);
Response.Write("<hr />");
rssURL = "http://rss.sina.com.cn/news/world/focus15.xml";
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />");
ProcessRSSItem(rssURL);
//
Response.Write("<hr />");
rssURL = "http://rss.sina.com.cn/news/china/focus15.xml";
Response.Write("<font size=5><b>Site: " + rssURL + "</b></font><Br />");
ProcessRSSItem(rssURL);
%>
</div>
</form>
</body>
</html>