温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在线实现RSS阅读器

发布时间:2020-06-15 08:25:45 来源:网络 阅读:480 作者:zhangyah 栏目:编程语言

1、通过XmlDocument类实现RSS信息获取

/// <summary>
/// 获得要显示的Feed种子数据
/// </summary>
/// <param name="RssUrl"></param>
/// <param name="showNewsCount"></param>
/// <returns></returns>
public string LoadRSS(string RssUrl, int showNewsCount)
{
string strRssList = "";
string strMsg;
try
{
XmlDocument objXMLDoc = new XmlDocument();
objXMLDoc.Load(RssUrl); //加载XML文档
XmlNodeList objItems = objXMLDoc.GetElementsByTagName("item");//获取所有匹配的元素
if (showNewsCount > 30)
showNewsCount = 10; //只显示10条记录
if (showNewsCount < 1)
showNewsCount = objItems.Count;
string title = "";
string link = "";
int i;
if (objXMLDoc.HasChildNodes == true) //该文档有子节点
{
i = 1;
foreach (XmlNode objNode in objItems) //循环所有元素
{
if (i <= showNewsCount)
{
if (objNode.HasChildNodes == true)
{
XmlNodeList objItemsChild = objNode.ChildNodes; //得到当前元素的所有子节点
foreach (XmlNode objNodeChild in objItemsChild)
{
switch (objNodeChild.Name)
{
case "title":
title = objNodeChild.InnerText;
break;
case "link":
link = objNodeChild.InnerText;
break;
}
}
i = i + 1;
strRssList += "<a href=" + link + " target=_blank>" + title + "</a><br>";
}
}
}
}
strMsg = strRssList;
}
catch
{
strMsg = "RSS Feed 源数据出错!";
}
return strMsg;
}

2、通过XMLHTTP无刷新更新在线RSS阅读获取

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无刷新动态更新的在线RSS阅读获取</title>
<script type="text/javascript">
var oDiv;
var xh = null;
function getXML() {
try {
xh = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xh = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (oc) {
xh = null
}
}
if (!xh && typeof XMLHttpRequest != "undefined") {
xh = new XMLHttpRequest();
}
oDiv = document.getElementById("rssitem");
oDiv.style.display = "";
try {
//如果是本地改成下面语句直接读取
xh.open("GET", "03-R***ss.xml", false);
xh.send(null);
loadData(xh, oDiv);
}
catch (e) {
error(oDiv, xh);
}
}
function loadData(xh, oDiv) //解析XML数据
{
if (xh.readyState == 4) //已完成
{
xml = xh.responsexml;
var i;
var nodes = xml.selectNodes("/rss/channel/item"); //查询子节点
var bloglink = xml.selectSingleNode("/rss/channel/link").text; //单个节点
oDiv.innerHTML = "";
var html;
for (i = 0; i < nodes.length; i++) {
html += "<div >";
html += "    <div >";
html += "        <a class=font1 href='" + nodes[i].selectSingleNode("link").text + "' target='_blank'>" + nodes[i].selectSingleNode("title").text + "</a><BR><BR>";
html += "    </div>";
html += "    <div >";
html += "        " + nodes[i].selectSingleNode("description").text;
html += "    </div>"
html += "</div><hr>";
}
oDiv.innerHTML = html;
}
else {
error(oDiv, xh);
}
}
function error(oDiv, xh) //错误处理
{
alert(xh.readyState);
if (xh.readyState == 4) {
if (xh.status == 200) {
oDiv.innerHTML = "完成"
}
else {
oDiv.innerHTML = "抱歉,装载数据失败。原因:" + xh.statusText
}
}
else {
oDiv.innerHTML = "载入失败。原因:";
}
}
</script>
</head>
<body onload="window.setTimeout('getXML()',200);">
<div align="center">
<table cellpadding="0" cellspacing="0" border="0" width="80%">
<tr>
<td>
<div id="rssitem" >
</div>
</td>
</tr>
</table>
</div>
</body>
</html>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI