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;
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。