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
?
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
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;
...
}
You're looking for the SyndicationFeed
class, which does exactly that.
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
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);
}
}
来源:https://stackoverflow.com/questions/10399400/best-way-to-read-rss-feed-in-net-using-c-sharp