我會添加一箇中繼器來填充來自RSS的項目,並且還使用Syndication(System.ServiceModel.Syndication)。我選擇了轉發器,因爲它具有非常靈活的佈局。如果你願意,你可以使用ListView或者GridView。我的標記應該是這樣的:
<asp:Repeater ID="rptRSS" runat="server" OnItemDataBound="rptRSS_ItemDataBound">
<HeaderTemplate>
<table class="tablerss">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="rsstitle">
<asp:Label ID="lblRSSTitle" runat="server" CssClass="rsshead"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:HyperLink ID="lnkArticle" runat="server"></asp:HyperLink>
</td>
</tr>
<tr>
<td class="rssdescription">
<asp:Label ID="lblDescription" runat="server"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
<br />
</FooterTemplate>
</asp:Repeater>
而且在我的代碼有:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadRSS();
}
}
protected void rptRSS_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HyperLink lnkArticle = (HyperLink)e.Item.FindControl("lnkArticle");
Label lblDescription = (Label)e.Item.FindControl("lblDescription");
Label lblRSSTitle = (Label)e.Item.FindControl("lblRSSTitle");
SyndicationItem item = (SyndicationItem)e.Item.DataItem;
lnkArticle.Text = item.Title.Text;
lnkArticle.NavigateUrl = item.Links[0].Uri.ToString(); ;
lblRSSTitle.Text = item.Title.Text;
lblDescription.Text = item.Summary.Text;
}
}
private void LoadRSS()
{
List<SyndicationItem> lstSynItem = new List<SyndicationItem>();
string uri = "http://www.weather.com/rss/national/rss_nwf_rss.xml?cm_ven=NWF&cm_cat=rss&par=NWF_rss";
SyndicationFeed myRss = SyndicationFeed.Load(XmlReader.Create(uri));
foreach (SyndicationItem item in myRss.Items)
{
lstSynItem.Add(item);
}
rptRSS.DataSource = lstSynItem;
rptRSS.DataBind();
}
有由Scott Michell一些好文章,雖然他們沒有更新,但仍有助於開始探索RSS!