我从我的数据库获取Rss Feed但是我得到了所有的提要,每次我在DB数据中运行我的代码时都在添加。我需要显示Rss Feed in Count以及如何忽略DB中已经添加的提要和我在Repeater控件中显示的源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
RssDC rssDC = new RssDC();
RssBAL rssBAL = new RssBAL();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
//List<RSSFeed> rssFeedList = new List<RSSFeed>();
//var item = BlogFeeds("http://rss.cnn.com/rss/edition_americas.rss", 3, rssFeedList);
//var item2 = BlogFeeds("http://feeds.bbci.co.uk/news/rss.xml?edition=int#", 3, rssFeedList);
//var item3 = BlogFeeds("http://feeds.reuters.com/reuters/Election2012", 3, rssFeedList);
//Repeater1.DataSource = item3;
//Repeater1.DataBind();
BlogFeeds("http://rss.cnn.com/rss/edition_americas.rss", 3);
fillRssFeeds();
}
}
private void fillRssFeeds()
{
DataSet ds = new DataSet();
ds = rssBAL.GetRssFeeds(rssDC);
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
protected void BlogFeeds(string url, int count)
{
try
{
RssDC rssDC = new RssDC();
XmlDocument xmldoc = new XmlDocument();
XmlNodeList items = default(XmlNodeList);
xmldoc.Load(url);
items = xmldoc.SelectNodes("/rss/channel/item");
// use XPath to get only items
foreach (XmlNode item1 in items)
{
foreach (XmlNode node1 in item1.ChildNodes)
{
if (node1.Name == "title")
{
rssDC.Title = node1.InnerText;
}
else if (node1.Name == "link")
{
rssDC.Link = node1.InnerText;
}
else if(node1.Name == "guid")
{
rssDC.RSS_ID = node1.InnerText;
}
else if (node1.Name == "description")
{
rssDC.Description = node1.InnerText;
}
else if (node1.Name == "pubDate")
{
rssDC.DatePublished = node1.InnerText;
}
}
rssDC = RssBAL.AddRssFeed(rssDC);
}
}
catch (Exception eax)
{
//return;
}
//return rssFeedList;
//Repeater1.DataSource = rssFeedList;
//Repeater1.DataBind();
}
答案 0 :(得分:1)
如果你只想根据你从BlogFeeds()提供的输入参数检索一定数量的文章,你可以尝试这样的事情:
foreach (XmlNode item1 in items)
{
int currentIterationIndex = 0;
foreach (XmlNode node1 in item1.ChildNodes)
{
if(currentIterationIndex >= count -1)
{
continue; //or break; would probably be better.
}
if (node1.Name == "title")
{
rssDC.Title = node1.InnerText;
}
else if (node1.Name == "link")
{
rssDC.Link = node1.InnerText;
}
else if(node1.Name == "guid")
{
rssDC.RSS_ID = node1.InnerText;
}
else if (node1.Name == "description")
{
rssDC.Description = node1.InnerText;
}
else if (node1.Name == "pubDate")
{
rssDC.DatePublished = node1.InnerText;
}
currentIterationIndex += 1;
}
rssDC = RssBAL.AddRssFeed(rssDC);
}
}