读取RSS(支持目前所有RSS版本)
2008-04-23 09:18
573 查看
using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.XPath;
public class RssDoc
...{
XmlDocument doc;
List<Hashtable> list;
public RssDoc()
...{
}
public RssDoc(XmlDocument doc)
...{
this.doc = doc;
Load();
}
/**//// <summary>
/// 获取网络资源
/// </summary>
/// <param name="url">url</param>
/// <param name="timeOut">timeout(单位秒)</param>
/// <param name="useProxy">是否用代理</param>
public static XmlDocument GetDoc(string url, int timeOut, bool useProxy)
...{
XmlDocument _doc = new XmlDocument();
try
...{
WebRequest req = WebRequest.Create(url);
if (useProxy)
...{
WebProxy proxy = new WebProxy("http://63.149.98.16:80/", true);
req.Proxy = proxy;
}
req.Timeout = timeOut * 1000;
WebResponse res = req.GetResponse();
Stream rssStream = res.GetResponseStream();
_doc.Load(rssStream);
rssStream.Dispose();
res.Close();
}
catch
...{
_doc = null;
}
return _doc;
}
private void Load()
...{
list = new List<Hashtable>();
XmlNodeList nodes = doc.GetElementsByTagName("item");
if (nodes==null||nodes.Count == 0)
nodes = doc.GetElementsByTagName("entry");
if (nodes == null || nodes.Count == 0)
return;
Hashtable ht;
XmlNodeList ns;
string name;
string date;
foreach (XmlNode node in nodes)
...{
ht = new Hashtable();
ns = node.ChildNodes;
try
...{
foreach (XmlNode n in ns)
...{
name = n.Name.ToLower();
if (name.Contains("link"))
...{
if (n.Attributes["href"] != null)
ht["link"] = n.Attributes["href"].Value.Trim();
else ht["link"] = n.InnerText.Trim();
continue;
}
if (name.Contains("title"))
...{
ht["title"] = n.InnerText.Trim();
continue;
}
if (name.Contains("category"))
...{
if (ht["category"] == null) ht["category"] = n.InnerText.Trim();
else ht["category"] = ht["category"].ToString() + "," + n.InnerText.Trim();
continue;
}
if (name.Contains("date"))
...{
date = n.InnerText;
if (date != "")
...{
if (date.Contains(",")) date = date.Substring(date.IndexOf(",") + 1);
date = date.Trim();
if (date.Split(' ').Length > 4) date = date.Replace(date.Split(' ')[4], "");
if (date.Contains(".")) date = date.Split('.')[0].Trim();
date = date.Replace("T", " ");
if (date.Substring(date.LastIndexOf(":") + 1).Length > 2)
date = date.Substring(0, date.LastIndexOf(":") + 3);
try ...{ date = DateTime.Parse(date.Trim()).ToString(); }
catch ...{ date = DateTime.Now.ToString(); }
}
else date = DateTime.Now.ToString();
ht["pubdate"] = date;
continue;
}
if (name.Contains("description"))
...{
ht["description"] = n.InnerText.Trim();
continue;
}
if (name.Contains("content"))
...{
ht["description"] = n.InnerText.Trim();
continue;
}
if (name.Contains("summary"))
...{
if (ht["description"] == null) ht["description"] = n.InnerText.Trim();
}
}
}
catch
...{
continue;
}
if (ht["link"] == null) ht["link"] = "";
if (ht["title"] == null || ht["title"].ToString() == "") ht["title"] = ht["link"].ToString();
if (ht["category"] == null) ht["category"] = "";
if (ht["pubdate"] == null) ht["pubdate"] = DateTime.Now.ToString();
if (ht["description"] == null) ht["description"] = "";
list.Add(ht);
}
}
public List<Hashtable> Items
...{
get
...{
return list;
}
}
}
使用:
XmlDocument doc = RssDoc.GetDoc(url, 3, false);
RssDoc rssdoc = new RssDoc(doc);
string title = "";
string link = "";
string description = "";
string cate = "";
string time = "";
foreach (Hashtable hs in rssdoc.Items)
...{
description = hs["description"].ToString();
title = hs["title"].ToString();
link = hs["link"].ToString();
time = hs["pubdate"].ToString();
cate = hs["category"].ToString();
}
相关文章推荐
- 读取RSS(支持目前所有RSS版本).net
- android 来电接听和挂断 支持目前所有版本
- java读取excel 支持所有版本
- 如何为自己的博客文章自动添加移动版本(目前仅支持博客园)
- 纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
- SpringBoot 2.0.0 注入SpingCloud 有bug(目前只有SpringBoot 1.5+ 版本的支持)
- IE系的Maxthon昨天发布1.2.00版本,支持RSS
- tlplayer 所有平台版本支持水印叠加
- 《OpenCV图像处理编程实例》所有代码已更新支持版本OpenCV3.1.0
- 一个JavaScript WEB日历控件,支持IE6,FireFox,可支持不同语言版本,目前支持中英文。
- PostgreSQL 10 Beta 3 发布,和对所有受支持版本的安全修复
- Mono发布2.4.3版本,支持C#4.0的所有功能
- tlplayer 所有平台版本支持水印叠加
- 联众的0day,目前通杀所有版本
- QQ2009聊天文本记录器 完成发布,支持目前最新的QQ2009 SP1版本。
- [应用代码] android 自动接听电话和挂断 (适合目前所有版本)
- [20081226更新(加了日文版本支持)]一个JavaScript WEB日历控件,支持IE6,FireFox,可支持不同语言版本,目前支持中文简繁,英,日语
- [置顶] 纯js读取excel文件内容,支持所有刘浏览器
- SERVER 2008 R2 SP1下的内存虚拟盘(支持32位,64位的所有windows版本)
- 编译KafkaOffsetMonitor-0.3-SNAPSHOT(此版本支持从broker读取)