C#編寫RSS彙總閱覽器[整理]

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.