2012-11-07 113 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文件中的每一個元素最後,你必須顯示你的飼料,無論你想。