我正在对使用 ASP/C# 构建的网站进行修改,其中一项任务是添加 2 个 RSS 提要的显示 - 一个来自网站的内部博客,另一个来自网站的 Twitter帐户。然而,我似乎不断地从这两个提要中获得空提要,即使我已经确认我指向提要的正确 URL。我的代码如下所示。
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);
}
页面上的代码是:
<ul>
<asp:ListView ID="twitterrssRepeater" runat="server">
<ItemTemplate>
<%# DisplayTwitterFeedItem((Container as ListViewDataItem).DataItem as System.ServiceModel.Syndication.SyndicationItem) %>
</ItemTemplate>
</asp:ListView>
</ul>
和
<asp:ListView ID="blogrssRepeater" runat="server">
<ItemTemplate>
<%# DisplayBlogFeedItem((Container as ListViewDataItem).DataItem as System.ServiceModel.Syndication.SyndicationItem) %>
</ItemTemplate>
</asp:ListView>
显然,我错过了一些东西。根据我所读到的内容,我知道我应该验证自己的身份才能查看 Twitter feed - 我有凭据,但不确定在加载 SyndicateFeed 时如何将其传递到 SyndicateFeed 中。
非常感谢任何提示、建议或进一步信息的指导。
最佳答案
这是一个我用来接收 Twitter feed 的简单示例(我只获取姓名、更新标题和 ID)
public class TwitterFeed
{
public string Name { get; set; }
public string Title { get; set; }
public string Id { get; set; }
}
然后是获取 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();
}
希望有帮助
关于asp.net - 在 ASP 和 C# 中使用 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3390446/