.NET MVC Linq -> XML RSS 阅读器:Twitter XML 解析错误
似乎无论我做什么,我都无法让我的 Twitter RSS 提要显示在我的视图中。我没有收到任何错误,并且 RSS 提要加载正确,我只是无法获取模型的信息...
这是我的 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; }
}
}
}
这是我的控制器:
public ActionResult Index()
{
var viewModel = new HomeRssFeedViewModel();
return View(viewModel);
}
这是我的视图:
%@ 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 LINQ 查询是问题所在。以下将起作用:
Your LINQ query is the problem. The following will work:
我怀疑问题出在你的XML解析上。您正确加载了推文,但
xmlTreeTwitter.Descendants
函数不返回任何内容,最终您的model.Tweets.Count()
等于0
这就是您在视图中没有得到任何输出的原因。确保您使用正确的命名空间和正确的选择器来解析 XML 响应。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 yourmodel.Tweets.Count()
equals to0
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.