我正在为我的网站获取 RssFeeds,并且它正在显示。但是如何从多个站点获取 RSS Feed,并且需要从第一个站点的三个 feed、第二个站点的三个 feed 等依次显示;主要来自 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)