我正在为我的网站获取 RssFeeds 并且它正在显示。但是如何从多个站点获取 RSS Feeds 并且需要从第一个站点三个 feeds,第二个站点三个 feeds 等在线显示;主要来自 CNN,BBC
这是我的代码:
protected void Page_Load(object sender, EventArgs e)
{
BlogFeeds();
}
protected void BlogFeeds()
{
try
{
XmlDocument xmldoc = new XmlDocument();
XmlNodeList items = default(XmlNodeList);
xmldoc.Load("http://rss.cnn.com/rss/edition_americas.rss");
xmldoc.Load("http://feeds.bbci.co.uk/news/rss.xml?edition=int#");
items = xmldoc.SelectNodes("/rss/channel/item");
// use XPath to get only items
string title = string.Empty;
string link = string.Empty;
string desc = string.Empty;
string pubDesc = string.Empty;
string st = "";
int i = 0;
foreach (XmlNode item1 in items)
{
foreach (XmlNode node1 in item1.ChildNodes)
{
if (node1.Name == "title")
{
title = node1.InnerText;
}
if (node1.Name == "link")
{
link = node1.InnerText;
}
if (node1.Name == "description")
{
desc = node1.InnerText;
if (desc.Length > 90)
{
pubDesc = desc.Substring(0, 90);
}
else
{ pubDesc = desc; }
}
}
st += "<a target='_blank' href='" + link + "'>" + title + "</a><br />" + pubDesc + " ... " + "<div style='border-bottom: 1px dotted #84acfd; padding-top:10px;'></div></br>";
i++;
if (i == 3)
break;
}
lblBlogOutput.Text = st;
}
catch (Exception eax)
{
return;
}
}
您不能调用 .Load() 并将其附加到 XmlDocument。
看看这个修改后的代码:
protected void Page_Load(object sender, EventArgs e)
{
BlogFeeds();
}
protected void BlogFeeds()
{
try
{
XmlDocument xmldoc = new XmlDocument();
XmlNodeList items = default(XmlNodeList);
xmldoc.Load("http://rss.cnn.com/rss/edition_americas.rss");
items = xmldoc.SelectNodes("/rss/channel/item");
ReadTopArticles(items);
xmldoc.Load("http://feeds.bbci.co.uk/news/rss.xml?edition=int#");
items = xmldoc.SelectNodes("/rss/channel/item");
ReadTopArticles(items);
}
catch (Exception eax)
{
return;
}
}
private void ReadTopArticles(XmlNodeList items)
{
string title = string.Empty;
string link = string.Empty;
string desc = string.Empty;
string pubDesc = string.Empty;
string st = "";
int i = 0;
foreach (XmlNode item1 in items)
{
foreach (XmlNode node1 in item1.ChildNodes)
{
if (node1.Name == "title")
{
title = node1.InnerText;
}
if (node1.Name == "link")
{
link = node1.InnerText;
}
if (node1.Name == "description")
{
desc = node1.InnerText;
if (desc.Length > 90)
{
pubDesc = desc.Substring(0, 90);
}
else
{ pubDesc = desc; }
}
}
st += String.Format("<a target='_blank' href='{0}'>{1}</a><br />{2} ... <div style='border-bottom: 1px dotted #84acfd; padding-top:10px;'></div></br>", link, title, pubDesc);
i += 1;
if (i == 3)
break;
}
lblBlogOutput.Text += st;
}
我是一名优秀的程序员,十分优秀!