views:

102

answers:

2

It seems like no matter what I do, I cannt get my twitter RSS feed to show up on my view. I'm not getting any errors, and the RSS feed loads correctly, I just can't grab the Model's information...

Here's my ViewModel

namespace MvcMusicStore.ViewModels
{
    public class HomeRssFeedViewModel
    {
        public IEnumerable<TwitterPosts> Tweets { get; set; }

        public HomeRssFeedViewModel()
        {
            Tweets = GetPosts();
        }

        public IEnumerable<TwitterPosts> GetPosts()
        {
            var xmlTreeTwitter = XDocument.Load("http://twitter.com/statuses/user_timeline/...");
            XNamespace xmlns = "http://www.w3.org/2005/Atom";

            return from item in xmlTreeTwitter.Descendants(xmlns + "item")
                   select new TwitterPosts
                   {
                       pubDate = item.Element(xmlns + "pubDate").Value,
                       Title = item.Element(xmlns + "Title").Value,
                       Link = item.Element(xmlns + "link").Value
                   };
        }
        public class TwitterPosts
        {
            public string pubDate { get; set; }
            public string Title { get; set; }
            public string Link { get; set; }
        }
    }
}

Here's my Controller:

public ActionResult Index()
{
    var viewModel = new HomeRssFeedViewModel();
    return View(viewModel);
}

Here's my View:

%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcMusicStore.ViewModels.HomeRssFeedViewModel>" %>

...
        <div class="rss">
           <% foreach(var tweet in Model.Tweets)
               {%>
                    <b><%: tweet.Title %></b>
              <% } %>
        </div>
+2  A: 

I suspect that the problem is in your XML parsing. You correctly load the tweets but the xmlTreeTwitter.Descendants function doesn't return anything and finally your model.Tweets.Count() equals to 0 which is the reason you are not getting any output in the view. Make sure you are using the correct namespace and proper selectors to parse the XML response.

Darin Dimitrov
+2  A: 

Your LINQ query is the problem. The following will work:

        public IEnumerable<TwitterPosts> GetPosts()
        {
            var xmlTreeTwitter = XDocument.Load("http://twitter.com/statuses/user_timeline/#####.rss");

            var v = from item in xmlTreeTwitter.Descendants("rss").Elements("channel").Elements("item")
                   select new TwitterPosts
                   {
                       pubDate = item.Element("pubDate").Value,
                       Title = item.Element("title").Value,
                       Link = item.Element("link").Value
                   };

            return v;
        }
        public class TwitterPosts
        {
            public string pubDate { get; set; }
            public string Title { get; set; }
            public string Link { get; set; }
        }
Dan