Question

In my application i will have dynamic rss feed url saved by users. so i want to know that how can i read that xml which will be returned by rss feed. what will be the structure of that xml ? I have reviewed some feed url and i noticed that most of them have title and description tags but i am not sure to this. if i get these two tags then i will parse xml but if they are not always available then how can i parse xml in that case.

these two contains title and description tag

http://rss.news.yahoo.com/rss/entertainment
http://xml.weather.yahoo.com/forecastrss?p=USCA1116
Was it helpful?

Solution

At first you need to read a XML file for that I recommend you to use XPath or Linq to XML, and as you already said there are three main elements that make up a feed; "title", "link" and "description".

Not a very long time ago I wrote a code to do that, I hope this works for you.

I created this two entities.

    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; }
    }

Then on this method I read every element from the XML file by using 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);
                }
            };
        }

And finally you have your feed to be displayed wherever you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top