Best Way to read rss feed in .net Using C#

2020-01-26 16:15发布

问题:

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?

回答1:

The System.ServiceModel.Syndication namespace has some stuff for you, namely the SyndicationFeed class.

This is a fairly simple example. http://blogs.msdn.com/b/steveres/archive/2008/01/20/using-syndicationfeed-to-displaying-photos-from-spaces-live-com.aspx



回答2:

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


回答3:

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



回答4:

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



回答5:

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

        }



    }