Example analysis of ASP.NET reading RSS

Y2J
Release: 2017-04-25 15:02:12
Original
1488 people have browsed it

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=&#39;" + link + "&#39; target=&#39;_blank&#39;>" + title + "</a> 发表于 "+createDate+"<hr/>"; 
            Rss += content; 
 
          } 
          if (i > RssCount) 
            break; 
        } 
      } 
    } 
    catch (Exception) 
    { 
      Rss = "RSS Feed 源数据出错!"; 
    } 
  } 
  else 
  { 
    Rss = "未找到信息源,您可刷新重试或联系管理员!"; 
  } 
  return Rss; 
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!