ASP.NET读取RSS的方法
RSS对于网站有着很重要的用途,本文即以实例展示了ASP.NET读取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; }
感兴趣的朋友可以测试并进一步完善本文所述实例代码,希望能对大家的ASP.NET程序设计有所帮助。
相关推荐
wanxuncpx 2020-06-01
malachuan 2020-05-17
IT前沿 2020-04-25
Vikingsoft 2020-04-23
晚安假行僧 2020-02-09
malachuan 2019-11-11
徐汇业大信息中心 2011-09-25
xiaomingqing 2015-06-29
chennai0 2015-07-22
panpanhappy 2019-07-15
iComputer 2019-09-05
川大门口贴膜小哥 2018-09-18
Vikingsoft 2018-08-18
IT前沿 2014-06-03
panpanhappy 2011-04-17
IT前沿 2013-08-27
灬扫地僧 2013-05-17