2012-11-07 69 views
0

在我的应用程序中,我将有动态RSS用户保存的饲料网址。所以我想知道我该如何读取RSS feed所返回的XML。那个xml的结构是什么?我已经审查了一些饲料网址,我注意到它们中的大多数都有titledescription标签,但我不确定这一点。如果我得到这两个标签,那么我会解析XML,但如果它们并不总是可用,那么我该如何解析XML。如何阅读动态rss饲料

这两个包含标题和描述标签

http://rss.news.yahoo.com/rss/entertainment 
http://xml.weather.yahoo.com/forecastrss?p=USCA1116 
+0

为了解析任何数据,你需要知道它的格式。要做到这一点,你需要找到它的规格。在这种情况下,谷歌RSS规范生病得到你所需要的,如http://cyber.law.harvard.edu/rss/rss.html。有一点需要考虑的是你是否在重新发明轮子。很有可能有人已经写了一些东西来解析RSS提要,你可以找到它。 http://stackoverflow.com/questions/576267/c-sharp-rss-reader也可能是一个有用的参考。 – Chris

回答

0

起初,你需要阅读应该是一个XML文件,我建议你使用XPath和LINQ到XML,并且你已经说主要有三个要素构成饲料; “标题”,“链接”和“描述”。

不是很久以前我写了一个代码来做到这一点,我希望这对你有用。

我创建了这两个实体。

public class RssFeed 
    { 
     public string Title { get; set; } 

     public string Link { get; set; } 

     public string Description { get; set; } 

     public string PubDate { get; set; } 

     public string Language { get; set; } 

     public ObservableCollection<RssItem> RssItems { get; set; } 
    } 

    public class RssItem 
    { 
     public string Title { get; set; } 

     public string Description { get; set; } 

     public string Link { get; set; } 
    } 

然后在这个方法我用Linq to XML

private static void ReadFeeds() 
     { 
      string uri = @"http://news.yahoo.com/rss/entertainment"; 

      WebClient client = new WebClient(); 
      client.DownloadStringAsync(new Uri(uri, UriKind.Absolute)); 

      client.DownloadStringCompleted += (s, a) => 
      { 
       if (a.Error == null && !a.Cancelled) 
       { 
        var rssReader = XDocument.Parse(a.Result); 

        var feed = (from rssFeed in rssReader.Descendants("channel") 
           select new RssFeed() 
           { 
            Title = null != rssFeed.Descendants("title").FirstOrDefault() ? 
              rssFeed.Descendants("title").First().Value : string.Empty, 

            Link = null != rssFeed.Descendants("link").FirstOrDefault() ? 
              rssFeed.Descendants("link").First().Value : string.Empty, 

            Description = null != rssFeed.Descendants("description").FirstOrDefault() ? 
              rssFeed.Descendants("description").First().Value : string.Empty, 

            PubDate = null != rssFeed.Descendants("pubDate").FirstOrDefault() ? 
              rssFeed.Descendants("pubDate").First().Value : string.Empty, 

            Language = null != rssFeed.Descendants("language").FirstOrDefault() ? 
              rssFeed.Descendants("language").First().Value : string.Empty 
           }).Single(); 

        var rssFeeds = (from rssItems in rssReader.Descendants("item") 
            select new RssItem() 
            { 
             Title = null != rssItems.Descendants("title").FirstOrDefault() ? 
               rssItems.Descendants("title").First().Value : string.Empty, 

             Link = null != rssItems.Descendants("link").FirstOrDefault() ? 
               rssItems.Descendants("link").First().Value : string.Empty, 

             Description = null != rssItems.Descendants("description").FirstOrDefault() ? 
               rssItems.Descendants("description").First().Value : string.Empty, 
            }).ToList(); 

        feed.RssItems = new ObservableCollection<RssItem>(rssFeeds); 
       } 
      }; 
     } 

读取XML文件中的每一个元素最后,你必须显示你的饲料,无论你想。