ASP.NET讀取RSS

來源:互聯網
上載者:User

標籤:des   blog   http   io   資料   for   ar   art   

從網上找的一段讀取RSS的代碼,經測能用: 
C#代碼  
  1. /// <summary>  
  2. /// 載入RSS  
  3. /// </summary>  
  4. /// <param name="RssUrl">RSS地址</param>  
  5. /// <param name="RssCount">要提取的文章數量</param>  
  6. /// <returns></returns>  
  7. public string LoadRSS(string RssUrl, int RssCount)  
  8. {  
  9.     XmlDocument doc = new XmlDocument();  
  10.     string Rss = "";  
  11.     if (RssUrl != "")  
  12.     {  
  13.         try  
  14.         {  
  15.             doc.Load(RssUrl);  
  16.             XmlNodeList nodelist = doc.GetElementsByTagName("item");  
  17.             XmlNodeList objItems1;  
  18.             int i = 1;  
  19.             if (doc.HasChildNodes)  
  20.             {  
  21.                 foreach (XmlNode node in nodelist)  
  22.                 {  
  23.                     string title = ""; // 文章標題  
  24.                     string link = "";  // 連結  
  25.                     string content = ""; // 內容  
  26.                     string createDate = ""; // 發表時間  
  27.                     i += 1;  
  28.                     if (node.HasChildNodes)  
  29.                     {  
  30.                         objItems1 = node.ChildNodes;  
  31.                         foreach (XmlNode node1 in objItems1)  
  32.                         {  
  33.                             switch (node1.Name)  
  34.                             {  
  35.                                 case "title":  
  36.                                     title = node1.InnerText;  
  37.                                     break;  
  38.                                 case "link":  
  39.                                     link = node1.InnerText;  
  40.                                     break;  
  41.                                 case "description":  
  42.                                     content = node1.InnerText;  
  43.                                     break;  
  44.                                 case "pubDate":  
  45.                                     createDate = node1.InnerText;  
  46.                                     break;  
  47.                             }  
  48.                             if (title != "" && link != "")  
  49.                                 break;  
  50.                         }  
  51.                         Rss += "<a href=‘" + link + "‘ target=‘_blank‘>" + title + "</a> 發表於 "+createDate+"<hr/>";  
  52.                         Rss += content;  
  53.   
  54.                     }  
  55.                     if (i > RssCount)  
  56.                         break;  
  57.                 }  
  58.             }  
  59.         }  
  60.         catch (Exception)  
  61.         {  
  62.             Rss = "RSS Feed 來源資料出錯!";  
  63.         }  
  64.     }  
  65.     else  
  66.     {  
  67.         Rss = "未找到資訊源,您可重新整理重試或聯絡管理員!";  
  68.     }  
  69.     return Rss;  
  70. }  
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.