我正在對使用ASP/C#構建的站點進行修改,其中一項任務是添加2個RSS提要的顯示 - 一個來自站點的內部博客,另一個來自該網站的Twitter帳戶。不過,我似乎不斷地從兩者獲取空的提要,即使我已經確認我指向提要的正確網址。我的代碼如下所示。在ASP和C中使用RSS提要
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供稿 - 我擁有憑據,但不知道如何在加載它時將它們傳遞到SyndicationFeed。
任何有關進一步信息的提示,建議或方向將不勝感激。
我在代碼中看到的唯一的區別是圍繞飼料的URL中添加的String.format()的。我嘗試添加它,結果與我沒有看到任何顯示的結果相同,儘管事實上我確信這些提要都包含項目。 – Elie 2010-08-03 00:25:52
嗯,我幾乎處於虧損狀態,這對我的Twitter Feed起作用。 編輯:當看着你的代碼與我的我們使用不同的URL,你使用http://twitter.com/statuses/user_timeline/84668697.rss,我使用http://search.twitter。 ?COM/search.atom q =到:psychocoder。 讓我試着用你正在使用的URL,看看會發生什麼 – PsychoCoder 2010-08-03 00:43:51
仔細研究一下,這裏的問題似乎是,當我檢索feed並將它綁定到ListView之間存在斷開。我嘗試將幾個項目強制到列表中,並且我仍然沒有任何內容,所以問題可能不是RSS相關的,但與ListView相關。 – Elie 2010-08-03 01:19:58