桦山涧

桦山涧
Asp.net ---->知识改变命运!
posts - 261, comments - 171, trackbacks - 6, articles - 3
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#编写RSS聚合阅览器[整理]

Posted on 2008-05-07 14:22 桦林 阅读(...) 评论(...) 编辑 收藏

<%@ 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>