读取RSS(支持目前所有RSS版本)
来源:互联网 发布:数据球神 编辑:程序博客网 时间:2024/05/23 18:09
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;
}
}
}
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();
}
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版本)
- 读取RSS(支持目前所有RSS版本).net
- RSS读取
- php 读取rss (2)
- RSS
- RSS
- rss
- RSS
- RSS
- RSS
- rss
- Rss
- rss
- RSS
- rss
- RSS
- RSS?
- RSS
- linux&unix软件的安装方式 汇集
- ZwSetSystemInformation的SystemLoadAndCallImage 加载驱动的缺陷
- CSDN又改版了……连个招呼都不打
- VisualFC 0.80 beta1 Released!
- [编程难题]编写一个求解时间满足约束的的算法.说明内详
- 读取RSS(支持目前所有RSS版本)
- Groovy Tip 6 集合类的简化操作
- http header详解
- asp.net(c#)给控件添加事件
- SQL 关于tree存储及提取的话题
- 20种好习惯 让你每天都快乐
- BREW是什么?
- http错误代码含义
- PE文件感染学习之一__通过现成代码加入一节信息来研究EXE文件的变化