Elie
Elie

Reputation: 13855

Consuming RSS feeds in ASP and C#

I'm working on making a modification to a site built with ASP/C#, and one of the tasks was to add in the display of 2 RSS feeds - one from the site's internal blog, and the other from the site's Twitter account. However, I seem to continually get empty feeds from both, even though I've confirmed that I'm pointing to the correct URL of the feeds. My code is shown below.

private void GetTwitterRSS()
{
    IEnumerable items = Cache["TwitterFeed"] as List<SyndicationItem>;

    if (items == null)
    {
        try 
        {
            SyndicationFeed blogFeed = SyndicationFeed.Load(XmlReader.Create("http://twitter.com/statuses/user_timeline/84668697.rss"));
            items = blogFeed.Items;
        }
        catch 
        {
            items = new List<SyndicationItem>();
        }
        Cache.Insert("TwitterFeed", items, null, DateTime.Now.AddMinutes(5.0),TimeSpan.Zero);
        twitterrssRepeater.DataSource = items;
        twitterrssRepeater.DataBind();
    }
}

private void GetBlogRSS()
{
    IEnumerable items = Cache["BlogFeed"] as List<SyndicationItem>;

    if (items == null)
    {
        try 
        {
            SyndicationFeed blogFeed = SyndicationFeed.Load(XmlReader.Create("http://www.rentseeker.ca/blog/?feed=rss2"));
            items = blogFeed.Items;
        }
        catch 
        {
            items = new List<SyndicationItem>();
        }
        Cache.Insert("BlogFeed", items, null, DateTime.Now.AddHours(1.0),TimeSpan.Zero);
        blogrssRepeater.DataSource = items;
        blogrssRepeater.DataBind();
    }
}

protected string DisplayBlogFeedItem(SyndicationItem item) 
{
    return string.Format(@"<p>{1}</p><p><strong>{2}</strong></p><p>{3}</p>", 
        FormatPublishDate(item.PublishDate.DateTime),   
        item.Title.Text,
        item.Summary.Text);
}

protected string DisplayTwitterFeedItem(SyndicationItem item) 
{
    return string.Format(@"<li>{1}</li>",
        item.Title.Text);
}

The code on the page is:

<ul>
    <asp:ListView ID="twitterrssRepeater" runat="server">
        <ItemTemplate>
            <%# DisplayTwitterFeedItem((Container as ListViewDataItem).DataItem as System.ServiceModel.Syndication.SyndicationItem) %>
        </ItemTemplate>
    </asp:ListView>
</ul>

and

<asp:ListView ID="blogrssRepeater" runat="server">
    <ItemTemplate>
        <%# DisplayBlogFeedItem((Container as ListViewDataItem).DataItem as System.ServiceModel.Syndication.SyndicationItem) %>
    </ItemTemplate>
</asp:ListView>

Clearly, I'm missing something. From what I've read, I understand that I'm supposed to authenticate myself in order to view a Twitter feed - I have the credentials, but am not sure how to pass them into SyndicationFeed when I load it.

Any tips, suggestions, or direction for further information is greatly appreciated.

Upvotes: 2

Views: 3888

Answers (2)

Vadym Kyrylkov
Vadym Kyrylkov

Reputation: 630

SyndicationFeed.Items is not a List, but implements IEnumerable interface, so instead of

IEnumerable items = Cache["BlogFeed"] as List<SyndicationItem>;

use the following line:

IEnumerable items = Cache["BlogFeed"] as IEnumerable<SyndicationItem>;

Upvotes: 0

PsychoCoder
PsychoCoder

Reputation: 10765

Here's a simple example I would use to receive my Twitter feed (I'm only getting name, update title & id)

public class TwitterFeed
{
    public string Name { get; set; }
    public string Title { get; set; }
    public string Id { get; set; }
}

Then the method to get the feed

public List<TwitterFeed> GetTwitterFeed(string name)
{
    List<TwitterFeed> list = new List<TwitterFeed>();
    XmlReader reader = XmlReader.Create(string.Format("http://search.twitter.com/search.atom?q=to:{0}", name));
    SyndicationFeed feed = SyndicationFeed.Load(reader);
    var tweetItems = from item in feed.Items
            select new TwitterFeed()
            {
                Name = item.Authors.First().Name,
                Title = item.Title.Text,
                Id = item.Id
            };

    return tweetItems.ToList();
}

Hope that helps

Upvotes: 4

Related Questions