0

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
1
  • For parsing any data you need to knwo its format. To do this you need to find its specification. In this case google for RSS specification ill get you what you need such as cyber.law.harvard.edu/rss/rss.html. One thing to consider though is whether you are reinventing the wheel. Chances are somebody has already written something to parse RSS feeds and you can look for that. stackoverflow.com/questions/576267/c-sharp-rss-reader may also be a useful reference.
    – Chris
    Nov 7, 2012 at 17:35

1 Answer 1

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.