ASP.NET读取RSS
从网上找的一段读取RSS的代码,经测能用:
- /// <summary>
- /// 加载RSS
- /// </summary>
- /// <param name="RssUrl">RSS地址</param>
- /// <param name="RssCount">要提取的文章数量</param>
- /// <returns></returns>
- public string LoadRSS(string RssUrl, int RssCount)
- {
- XmlDocument doc = new XmlDocument();
- string Rss = "";
- if (RssUrl != "")
- {
- try
- {
- doc.Load(RssUrl);
- XmlNodeList nodelist = doc.GetElementsByTagName("item");
- XmlNodeList objItems1;
- int i = 1;
- if (doc.HasChildNodes)
- {
- foreach (XmlNode node in nodelist)
- {
- string title = ""; // 文章标题
- string link = ""; // 链接
- string content = ""; // 内容
- string createDate = ""; // 发表时间
- i += 1;
- if (node.HasChildNodes)
- {
- objItems1 = node.ChildNodes;
- foreach (XmlNode node1 in objItems1)
- {
- switch (node1.Name)
- {
- case "title":
- title = node1.InnerText;
- break;
- case "link":
- link = node1.InnerText;
- break;
- case "description":
- content = node1.InnerText;
- break;
- case "pubDate":
- createDate = node1.InnerText;
- break;
- }
- if (title != "" && link != "")
- break;
- }
- Rss += "<a href='" + link + "' target='_blank'>" + title + "</a> 发表于 "+createDate+"<hr/>";
- Rss += content;
- }
- if (i > RssCount)
- break;
- }
- }
- }
- catch (Exception)
- {
- Rss = "RSS Feed 源数据出错!";
- }
- }
- else
- {
- Rss = "未找到信息源,您可刷新重试或联系管理员!";
- }
- return Rss;
- }