C#中解析Rss实现思路及通用类--源代码
来源:互联网 发布:软件开发工程师待遇 编辑:程序博客网 时间:2024/05/16 10:11
Rss是 Web 2.0 时代的主要特征,那么有没有一种通用的方法来解析Rss呢?
当然有了,呵呵。下面给出了一种思路,仅供参考!
主要用到了几个类文件:
Channel.cs 、ChannelCollection.cs 、 Feed.cs 、 Item.cs 、 ItemCollection.cs
下面给出各个类的源文件:
1、Channel.cs 类
using System;
namespace Utility.Rss
...{
/**////<summary>
/// channel
/// </summary>
[Serializable()]
public class Channel
...{
private string _title;
private string _link;
private string _description;
private ItemCollection items= new ItemCollection();
属性#region 属性
/**////<summary>
/// 标题
/// </summary>
public string title
...{
get...{return _title;}
set...{_title= value.ToString();}
}
/**////<summary>
/// 链接
/// </summary>
public string link
...{
get...{return _link;}
set...{_link= value.ToString();}
}
/**////<summary>
/// 描述
/// </summary>
public string description
...{
get...{return _description;}
set...{_description= value.ToString();}
}
public ItemCollection Items
...{
get ...{return items; }
}
#endregion
public Channel()...{}
}//
}//
namespace Utility.Rss
...{
/**////<summary>
/// channel
/// </summary>
[Serializable()]
public class Channel
...{
private string _title;
private string _link;
private string _description;
private ItemCollection items= new ItemCollection();
属性#region 属性
/**////<summary>
/// 标题
/// </summary>
public string title
...{
get...{return _title;}
set...{_title= value.ToString();}
}
/**////<summary>
/// 链接
/// </summary>
public string link
...{
get...{return _link;}
set...{_link= value.ToString();}
}
/**////<summary>
/// 描述
/// </summary>
public string description
...{
get...{return _description;}
set...{_description= value.ToString();}
}
public ItemCollection Items
...{
get ...{return items; }
}
#endregion
public Channel()...{}
}//
}//
2、ChannelCollection.cs 类
using System;
namespace Utility.Rss
...{
/**////<summary>
/// rssChannelCollection 的摘要说明。
/// </summary>
public class ChannelCollection : System.Collections.CollectionBase
...{
public Channelthis[int index]
...{
get
...{
return ((Channel)(List[index]));
}
set
...{
List[index]= value;
}
}
public int Add(Channel item)
...{
return List.Add(item);
}
public ChannelCollection()
...{
}
}//
}//
namespace Utility.Rss
...{
/**////<summary>
/// rssChannelCollection 的摘要说明。
/// </summary>
public class ChannelCollection : System.Collections.CollectionBase
...{
public Channelthis[int index]
...{
get
...{
return ((Channel)(List[index]));
}
set
...{
List[index]= value;
}
}
public int Add(Channel item)
...{
return List.Add(item);
}
public ChannelCollection()
...{
}
}//
}//
3、Feed.cs类
using System;
using System.Xml;
using System.IO;
using System.Net;
using System.Text;
namespace Utility.Rss
...{
/**////<summary>
/// rssFeed 的摘要说明。
/// </summary>
public class Feed
...{
private string _url;
private System.DateTime _lastModified;
private System.DateTime _lastRssDate;
private Channel channel= new Channel();
公共属性#region 公共属性
public string url
...{
get...{return _url;}
set...{_url=value;}
}
public System.DateTime lastModified
...{
get...{return _lastModified;}
}
public System.DateTime lstRssDate
...{
set...{_lastRssDate=value;}
}
public Channel Channel
...{
get ...{return channel; }
}
#endregion
public Feed()
...{
}
public Feed(string url,System.DateTime dt)
...{
this._url=url;
this._lastRssDate=dt;
}
public void Read()
...{
XmlDocument xDoc=new XmlDocument();
HttpWebRequest request= (HttpWebRequest)WebRequest.Create(_url);
request.Timeout=15000;
request.UserAgent=@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
Stream stream;
HttpWebResponse response= (HttpWebResponse)request.GetResponse();
this._lastModified= response.LastModified;
stream= response.GetResponseStream();
StreamReader sr;
//System.Xml.XmlReader = new XmlReader();
//stream=Encoding.Convert(Encoding.GetEncoding("GBK"),Encoding.GetEncoding("gb2312"),Convert.ToSByte(stream));
if(this.Get_CH(response.Headers["Content-Type"].ToString())=="GBK")
...{
sr=new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));
xDoc.Load(sr);
}
else
...{
// sr= new StreamReader(stream,System.Text.Encoding.UTF8);
xDoc.Load(stream);
}
if(this._lastRssDate<this._lastModified)
...{
XmlNodeList xnList=xDoc.DocumentElement["channel"].SelectNodes("item");
// XmlNodeList xnList=xDoc.SelectNodes("items");
int a= xnList.Count;
foreach(XmlNode xNodein xnList)
...{
Item rt=new Item();
rt.title=xNode.SelectSingleNode("title").InnerText.Replace("'","''");
rt.link=xNode.SelectSingleNode("link").InnerText.Replace("'","''");
rt.description=xNode.SelectSingleNode("description").InnerText.Replace("'","''");
try
...{
rt.pubDate=xNode.SelectSingleNode("pubDate").InnerText;
}
catch
...{
rt.pubDate=this._lastModified.ToString();
}
channel.Items.Add(rt);
}
}
}
public string Create()
...{
return "";
}
private string Get_CH(string s)
...{
int l=s.IndexOf("charset=")+8;
return s.Substring(l,s.Length-l);
}
}//
}//
using System.Xml;
using System.IO;
using System.Net;
using System.Text;
namespace Utility.Rss
...{
/**////<summary>
/// rssFeed 的摘要说明。
/// </summary>
public class Feed
...{
private string _url;
private System.DateTime _lastModified;
private System.DateTime _lastRssDate;
private Channel channel= new Channel();
公共属性#region 公共属性
public string url
...{
get...{return _url;}
set...{_url=value;}
}
public System.DateTime lastModified
...{
get...{return _lastModified;}
}
public System.DateTime lstRssDate
...{
set...{_lastRssDate=value;}
}
public Channel Channel
...{
get ...{return channel; }
}
#endregion
public Feed()
...{
}
public Feed(string url,System.DateTime dt)
...{
this._url=url;
this._lastRssDate=dt;
}
public void Read()
...{
XmlDocument xDoc=new XmlDocument();
HttpWebRequest request= (HttpWebRequest)WebRequest.Create(_url);
request.Timeout=15000;
request.UserAgent=@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.40607; .NET CLR 1.1.4322)";
Stream stream;
HttpWebResponse response= (HttpWebResponse)request.GetResponse();
this._lastModified= response.LastModified;
stream= response.GetResponseStream();
StreamReader sr;
//System.Xml.XmlReader = new XmlReader();
//stream=Encoding.Convert(Encoding.GetEncoding("GBK"),Encoding.GetEncoding("gb2312"),Convert.ToSByte(stream));
if(this.Get_CH(response.Headers["Content-Type"].ToString())=="GBK")
...{
sr=new StreamReader(stream,System.Text.Encoding.GetEncoding("GB2312"));
xDoc.Load(sr);
}
else
...{
// sr= new StreamReader(stream,System.Text.Encoding.UTF8);
xDoc.Load(stream);
}
if(this._lastRssDate<this._lastModified)
...{
XmlNodeList xnList=xDoc.DocumentElement["channel"].SelectNodes("item");
// XmlNodeList xnList=xDoc.SelectNodes("items");
int a= xnList.Count;
foreach(XmlNode xNodein xnList)
...{
Item rt=new Item();
rt.title=xNode.SelectSingleNode("title").InnerText.Replace("'","''");
rt.link=xNode.SelectSingleNode("link").InnerText.Replace("'","''");
rt.description=xNode.SelectSingleNode("description").InnerText.Replace("'","''");
try
...{
rt.pubDate=xNode.SelectSingleNode("pubDate").InnerText;
}
catch
...{
rt.pubDate=this._lastModified.ToString();
}
channel.Items.Add(rt);
}
}
}
public string Create()
...{
return "";
}
private string Get_CH(string s)
...{
int l=s.IndexOf("charset=")+8;
return s.Substring(l,s.Length-l);
}
}//
}//
4、Item.cs类
using System;
namespace Utility.Rss
...{
/**////<summary>
/// rssItem 的摘要说明。
/// </summary>
public class Item
...{
private string _title;
private string _link;
private string _description;
private string _pubDate;
属性#region 属性
/**////<summary>
/// 标题
/// </summary>
public string title
...{
get...{return _title;}
set...{_title=value.ToString();}
}
/**////<summary>
/// 链接
/// </summary>
public string link
...{
get...{return _link;}
set...{_link=value.ToString();}
}
/**////<summary>
/// 描述
/// </summary>
public string description
...{
get...{return _description;}
set...{_description=value.ToString();}
}
/**////<summary>
/// 频道内容发布日期
/// </summary>
public string pubDate
...{
get...{return _pubDate;}
set...{_pubDate=C_Date(value);}
}
#endregion
public Item()...{}
private string C_Date(string input)
...{
System.DateTime dt;
try
...{
dt=Convert.ToDateTime(input);
}
catch
...{
dt=System.DateTime.Now;
}
return dt.ToString();
}
}//
}//
namespace Utility.Rss
...{
/**////<summary>
/// rssItem 的摘要说明。
/// </summary>
public class Item
...{
private string _title;
private string _link;
private string _description;
private string _pubDate;
属性#region 属性
/**////<summary>
/// 标题
/// </summary>
public string title
...{
get...{return _title;}
set...{_title=value.ToString();}
}
/**////<summary>
/// 链接
/// </summary>
public string link
...{
get...{return _link;}
set...{_link=value.ToString();}
}
/**////<summary>
/// 描述
/// </summary>
public string description
...{
get...{return _description;}
set...{_description=value.ToString();}
}
/**////<summary>
/// 频道内容发布日期
/// </summary>
public string pubDate
...{
get...{return _pubDate;}
set...{_pubDate=C_Date(value);}
}
#endregion
public Item()...{}
private string C_Date(string input)
...{
System.DateTime dt;
try
...{
dt=Convert.ToDateTime(input);
}
catch
...{
dt=System.DateTime.Now;
}
return dt.ToString();
}
}//
}//
5、ItemCollection.cs类
using System;
namespace Utility.Rss
...{
/**////<summary>
/// rssChannelCollection 的摘要说明。
/// </summary>
public class ItemCollection : System.Collections.CollectionBase
...{
public Item this[int index]
...{
get ...{return ((Item)(List[index])); }
set
...{
List[index]= value;
}
}
public int Add(Item item)
...{
return List.Add(item);
}
public ItemCollection()
...{
}
}//
}//
namespace Utility.Rss
...{
/**////<summary>
/// rssChannelCollection 的摘要说明。
/// </summary>
public class ItemCollection : System.Collections.CollectionBase
...{
public Item this[int index]
...{
get ...{return ((Item)(List[index])); }
set
...{
List[index]= value;
}
}
public int Add(Item item)
...{
return List.Add(item);
}
public ItemCollection()
...{
}
}//
}//
注:
应用实例请参考这里
仅供参考,欢迎交换意见 :Com.LXJ@163.com
http://blog.csdn.net/lxjhb/article/details/1516172
- C#中解析Rss实现思路及通用类--源代码
- C#中解析Rss实现思路及通用类–源代码
- Max Subarray思路解析及源代码
- C#实现Access通用底层数据库类【源代码】
- c#解析RSS新闻源
- jsp中实现读取RSS——SAX解析XML
- 正则表达实现rss解析
- js解析RSS类
- C#通用字符串解析
- SameTree解法思路及源代码
- C#操作RSS 实现订阅
- IOS开发中rss解析数据解析
- C# 多文件重命名思路及实现 上:步骤
- C# 多文件重命名思路及实现 下:实例
- C# 多文件重命名思路及实现 下:实例
- Visual C#类的定义及实现方法实例解析
- Plus One思路解析和源代码
- File类源代码解析及使用
- 一个dshow filter的code心得。很简单,但写的还不错
- 企业网站建设需要注意的事
- 编程中常用的一些小工具及说明
- vim的配置文件在哪?
- spring事务控制小笔记
- C#中解析Rss实现思路及通用类--源代码
- 求字符串中单词的个数
- debug 没有错,release出错
- error LNK2001: unresolved external symbol 错误分析
- jQuery
- Java 打印六角星
- C# WebServices部署
- MFC中CString.Format的详细用法
- 虚拟化技术概要之VMM结构