例子,读取RSS文件内容。
代码:
//读取RSS函数LoadRSS
//参数RssURl 为RSS路径
//参数RssCount为读取的RSS条数
public string LoadRSS(string RssUrl , int RssCount)
{
XmlDocument doc = new XmlDocument();
string Rss = "";
if (RssUrl != "")
{
try
{
doc.Load(RssUrl);
XmlNodeList nodelist = doc.GetElementsByTagName("item");
XmlNodeList objItems1;
int i = 0;
if (doc.HasChildNodes)
{
foreach (XmlNode node in nodelist)
{
string title = "";
string link = "";
i += 1;
if (node.HasChildNodes)
{
objItems1 = node.ChildNodes;
foreach (XmlNode node1 in objItems1)
{
switch (node1.Name)
{
case "title":
title = node1.InnerText;
break;
case "link":
link = node1.InnerText;
break;
}
if (title != "" && link != "")
break;
}
Rss += "<a href='" + link + "' target='_blank'>" + title + "</a><br>";
}
if (i > RssCount)
break;
}
}
}
catch (Exception)
{
Rss = "RSS Feed 源数据出错!";
}
}
else
{
Rss = "未找到信息源,您可刷新重试或联系管理员!";
}
return Rss;
}