113

What is the best way to read RSS feeds?

I am using XmlTextReader to achieve this. Is there any other best way to do it?

XmlTextReader reader = new XmlTextReader(strURL);

DataSet ds = new DataSet();
ds.ReadXml(reader);

After reading the RSS feed using XmlTextReader, is there any way I can populate data to ListItem instead of DataSet?

enter image description here

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Tronics
  • 1,438
  • 4
  • 22
  • 33

5 Answers5

198

Add System.ServiceModel in references

Using SyndicationFeed:

string url = "http://fooblog.com/feed";
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
foreach (SyndicationItem item in feed.Items)
{
    String subject = item.Title.Text;    
    String summary = item.Summary.Text;
    ...                
}
Artemix
  • 2,113
  • 2
  • 23
  • 34
dlopezgonzalez
  • 4,217
  • 5
  • 31
  • 42
  • 14
    This is the schizzle. Awesome and succinct. You also need to be using System.Xml; but that's fairly evident. For Blogger the url is currently "http://blogname.blogspot.com/feeds/posts/default" (Atom 1.0) and "http://blogname.blogspot.com/feeds/posts/default?alt=rss" for RSS 2.0. The SyndicationItem item has all the item properties you'll need. Just examine the object in the Visual Studio debugger and be illuminated. – secretwep Oct 29 '13 at 03:40
  • can we use XmlTextReader instead of xmlreader any performance difference? – Dragon Apr 13 '17 at 08:13
  • 13
    For .Net Core, the NuGet Package you want to install to use this is `System.ServiceModel.Syndication` – adam0101 Mar 01 '19 at 19:16
  • for 2019 this blows up every time I try to access the only version which is prerelease. – Clarence Aug 07 '19 at 16:37
  • For me the below error is thrown with the above code. System.Xml.XmlException: 'For security reasons DTD is prohibited in this XML document. To enable DTD processing set the DtdProcessing property on XmlReaderSettings to Parse and pass the settings into XmlReader.Create method.' – Kurkula Apr 23 '22 at 03:42
11

This is an old post, but to save people some time if you get here now like I did, I suggest you have a look at the CodeHollow.FeedReader package which supports a wider range of RSS versions, is easier to use and seems more robust. https://github.com/codehollow/FeedReader

emilast
  • 559
  • 1
  • 5
  • 11
9

You're looking for the SyndicationFeed class, which does exactly that.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

Use this :

private string GetAlbumRSS(SyndicationItem album)
    {

        string url = "";
        foreach (SyndicationElementExtension ext in album.ElementExtensions)
            if (ext.OuterName == "itemRSS") url = ext.GetObject<string>();
        return (url);

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string albumRSS;
        string url = "http://www.SomeSite.com/rss‎";
        XmlReader r = XmlReader.Create(url);
        SyndicationFeed albums = SyndicationFeed.Load(r);
        r.Close();
        foreach (SyndicationItem album in albums.Items)
        {

            cell.InnerHtml = cell.InnerHtml +string.Format("<br \'><a href='{0}'>{1}</a>", album.Links[0].Uri, album.Title.Text);
            albumRSS = GetAlbumRSS(album);

        }



    }
Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50
1

Update: This supports only with UWP - Windows Community Toolkit

There is a much easier way now. You can use the RssParser class. The sample code is given below.

public async void ParseRSS()
{
    string feed = null;

    using (var client = new HttpClient())
    {
        try
        {
            feed = await client.GetStringAsync("https://visualstudiomagazine.com/rss-feeds/news.aspx");
        }
        catch { }
    }

    if (feed != null)
    {
        var parser = new RssParser();
        var rss = parser.Parse(feed);

        foreach (var element in rss)
        {
            Console.WriteLine($"Title: {element.Title}");
            Console.WriteLine($"Summary: {element.Summary}");
        }
    }
}

For non-UWP use the Syndication from the namespace System.ServiceModel.Syndication as others suggested.

public static IEnumerable <FeedItem> GetLatestFivePosts() {
    var reader = XmlReader.Create("https://sibeeshpassion.com/feed/");
    var feed = SyndicationFeed.Load(reader);
    reader.Close();
    return (from itm in feed.Items select new FeedItem {
        Title = itm.Title.Text, Link = itm.Id
    }).ToList().Take(5);
}

public class FeedItem {
    public string Title {
        get;
        set;
    }
    public string Link {
        get;
        set;
    }
}
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140