This article mainly introduces the method of ASP.NET reading RSS. It is a very practical skill. Friends who need it can refer to it.
RSS has a very important use for websites. This article shows it with examples. The method of ASP.NET reading RSS is for your reference. The specific method is as follows:
The main function code is as follows:
/// <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; }
Interested friends can test and further improve the example code described in this article , I hope it can be helpful to everyone's ASP.NET programming.
The above is the detailed content of Example analysis of ASP.NET reading RSS. For more information, please follow other related articles on the PHP Chinese website!