2

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.

2 Answers 2

4

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

4
  • The only difference I saw in the code was the addition of string.Format() around the URL of the feed. I tried adding it, with the same result of my not seeing anything displayed, despite the fact that I am positive the feeds both contain items.
    – Elie
    Aug 3, 2010 at 0:25
  • Hmm I'm almost at a loss, that works for my Twitter feed. EDIT: When looking at your code versus mine we're using different URL's, you're using twitter.com/statuses/user_timeline/84668697.rss and I'm using search.twitter.com/search.atom?q=to:psychocoder. Let me try it with the URL you're using and see what happens Aug 3, 2010 at 0:43
  • On closer examination, it seems that the issue here is that there is a disconnect between when I retrieve the feed and binding it to the ListView. I tried forcing in a few items into the list, and I still get nothing, so the issue may not be RSS related, but ListView related.
    – Elie
    Aug 3, 2010 at 1:19
  • Now I feel real stupid. Forgot to actually call GetBlogRSS, GetTwitterRSS. Thanks for the help, though.
    – Elie
    Aug 3, 2010 at 2:19
0

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

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.