*/ namespace microshaoft { using system; using system.xml; using system.text; using system.reflection; using system.collections; using system.text.regularexpressions;
*/ namespace microshaoft { using system; using system.xml; using system.text; using system.reflection; using system.collections; using system.text.regularexpressions;
public class rssheader { //feed url public rssheader(string url) { this._url = url; }
public string title { get { return this._title; } }
public string description { get { return this._description; } }
public string link { get { return this._link; } }
public string language { get { return this._language; } }
public string generator { get { return this._generator; } }
public string ttl { get { return this._ttl; } }
public string copyright { get { return this._copyright; } }
public datetime pubdate { get { return util.parsedatetime(this._pubdate); } }
public string category { get { return this._category; } }
public datetime lastbuilddate { get { return util.parsedatetime(this._lastbuilddate); } } public string managingeditor { get { return this._managingeditor; } }
public string url { get { return this._url; } }
public string dclanguage { get { return this._dclanguage; } }
public string trackbackping { get { return this._trackbackping; } }
public string wfwcommentrss { get { return this._wfwcommentrss; } }
public string wfwcomment { get { return this._wfwcomment; } }
public string slashcomments { get { return this._slashcomments; } } public string title { get { return this._title; } }
public string link { get { return this._link; } }
public string description { get { return this._description; } }
public string category { get { return this._category; } }
public string author { get { return this._author; } }
public datetime pubdate { get { return util.parsedatetime(this._pubdate); } }
public string comments { get { return this._comments; } }
public string guid { get { return this._guid; } } } public class simplerssreader { //rssheader header 解析处理完毕事件 public delegate void rssheaderreceiveeventhandler(simplerssreader sender, rssheader header); public event rssheaderreceiveeventhandler rssheaderreceive;
//某一个 rssitem 解析处理完毕事件 public delegate void rssitemreceiveeventhandler(simplerssreader sender, rssitem item); public event rssitemreceiveeventhandler rssitemreceive;
private type _trs; //typeof(rssheader) private type _tri; //typeof(rssitem)
private arraylist _rssitemsal;
private rssheader _rs; public rssheader rssheader { get { return this._rs; } }
//用于存储所有的 rssitem private rssitem[] _rssitems;
public rssitem[] rssitems { get { return this._rssitems; } }
public void rss(string url) { xmldocument xd = new xmldocument(); //如果效率不高可采用 webrequest 替代 xd.load(url); xmlnodelist xnl = xd.selectnodes("/rss/channel");
foreach (xmlnode xn in xnl) { //递归遍历 this.travel(xn, 0); }
if (this._rssitemsal.count > 0) { this._rssitems = new rssitem[this._rssitemsal.count]; int i = 0; foreach (object o in this._rssitemsal) { this._rssitems[i++] = (rssitem) o; } } }
/// <header> /// 递归遍历 /// </header> /// <param name="xn">节点</param> /// <param name="i">项目数</param> private void travel(xmlnode xn, int i) { if (xn.haschildnodes) { foreach (xmlnode x in xn.childnodes) { if (x.parentnode != null) { if (x.parentnode.name == "channel") { if (x.name == "item") { i ++; if (i >= 1) { xmlnode node = null; bool b = false; //是否是 rss item rssitem ri = null; if (i == 1) //header { node = xn; b = false; } else if (i > 1) //item { node = x; b = true; ri = new rssitem(); }
foreach (xmlnode n in node.childnodes) { if (n.name != "item") { if (!b) //rss header header { //根据 xml 实际存在的属性,利用反射为 rssheader 实例的私有成员赋值 fieldinfo fi = this._trs.getfield("_" + n.name.replace(":","") ,bindingflags.nonpublic | bindingflags.instance | bindingflags.public); if (fi != null) { fi.setvalue(this._rs,n.innertext); } } else //rss item { //根据 xml 实际存在的属性,利用反射为 rssitem 实例的私有成员赋值 fieldinfo fi = this._tri.getfield("_" + n.name.replace(":",""),bindingflags.nonpublic | bindingflags.instance | bindingflags.public); if (fi != null) { fi.setvalue(ri,n.innertext); } }
} } if (!b) { //触发 rssheaderreceive 事件 if (this.rssheaderreceive != null) { this.rssheaderreceive(this,this._rs); } } else { //制定 rssitem 实例的 header/header fieldinfo fi = this._tri.getfield("_header",bindingflags.nonpublic | bindingflags.instance | bindingflags.public); if (fi != null) { fi.setvalue(ri,this._rs); }
string s = html; for (int i = 0; i < regexs.length; i++) { s = new regex(regexs[i], regexoptions.multiline | regexoptions.ignorecase).replace(s, replaces[i]); } s.replace("<", ""); s.replace(">", ""); s.replace("/r/n", ""); return s; } } }
//测试程序 namespace test { using system; using system.data; using system.reflection; using system.data.sqlclient;
using microshaoft; using microshaoft.data;
class consoleapplication { private sqlconnection _connection; public string _channel;
public sqlconnection connection { set { this._connection = value; } get { return this._connection; } }
static void main() {
string s = "http://www.ccw.com.cn/rss/news2/1.xml"; s = "http://dzh.mop.com/topic/rss.jsp?type=28"; s = "http://www.ccw.com.cn/rss/news2/15.xml"; s = "http://www.cnblogs.com/rss.aspx?id=-1"; s = "http://localhost/rss.xml"; //s = "http://weblog.siliconvalley.com/column/dangillmor/index.xml"; //s= "http://www.skyone.com.cn/sub/rss/list_jjsc.xml";
consoleapplication a = new consoleapplication();
a.connection = new sqlconnection("server=server//psqlke;user id=sa;password=;database=rss"); a.connection.open();
simplerssreader srr = new simplerssreader();
srr.rssheaderreceive += new microshaoft.simplerssreader.rssheaderreceiveeventhandler(a.srr_rssheaderreceive); srr.rssitemreceive +=new microshaoft.simplerssreader.rssitemreceiveeventhandler(a.srr_rssitemreceive);
for (int i = 0; i < spa.length; i++) { //保存 参数名称与其位置(次序) 的关系 ht.add(spa[i].parametername.tolower().replace("@", ""), i);
//相当于为存储过程的所有参数赋初值 spa[i].value = null; }
//得到所有的属性 propertyinfo[] pi = t.getproperties(); foreach (propertyinfo x in pi) { if (ht.containskey( x.name.tolower())) { //根据参数(属性)名称得到参数的次序! int i = (int) ht[x.name.tolower()]; if (spa[i].direction == system.data.parameterdirection.input || spa[i].direction == system.data.parameterdirection.inputoutput) { object o; if (x.propertytype.name == "string") { o = x.getvalue(instance,null); if (o != null) { string s = util.striphtml((string) o); o = s; } } else { o = x.getvalue(instance,null); }
spa[i].value = o; } }
}
if (t == typeof(rssitem)) { spa[0].value = ((rssitem) instance).header.url; }
sqlhelper.executenonquery(this.connection, commandtype.storedprocedure, sp, spa); if (spa[spa.length - 1].value != system.dbnull.value) { system.console.writeline("save to id: {0} successful!", spa[spa.length - 1].value); } else { system.console.writeline("save failed! may be duplicate!"); } } } }
//========================================================================================================== /* --sql script if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sp_addchannel]') and objectproperty(id, n'isprocedure') = 1) drop procedure [dbo].[sp_addchannel] go
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[sp_addchannelsdetails]') and objectproperty(id, n'isprocedure') = 1) drop procedure [dbo].[sp_addchannelsdetails] go
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[channels]') and objectproperty(id, n'isusertable') = 1) drop table [dbo].[channels] go
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[channelsdetails]') and objectproperty(id, n'isusertable') = 1) drop table [dbo].[channelsdetails] go
create proc sp_addchannel @url varchar(8000) ,@link varchar(8000) ,@channel varchar(8000) ,@title varchar(8000) ,@image varchar(8000) ,@description varchar(7999) ,@language varchar(8000) ,@generator varchar(8000) ,@ttl varchar(8000) ,@copyright varchar(8000) ,@pubdate datetime ,@category varchar(8000) ,@docs varchar(8000) ,@managingeditor varchar(8000) ,@dclanguage varchar(8000) ,@ int out as set @ = 0 insert into channels ([url],[channel],[title],[description],[link],[language],[generator],[ttl],[copyright],[pubdate],[category],[dclanguage]) select @url,@channel,@title,@description,@link,@language,@generator,@ttl,@copyright,@pubdate,@category,@dclanguage where not exists(select 1 from channels where [url] = @url) select @ = scope_identity() go set quoted_identifier off go set ansi_nulls on go
set quoted_identifier on go set ansi_nulls on go
create proc sp_addchannelsdetails @url varchar(8000) ,@title varchar(8000) ,@description varchar(7000) ,@link varchar(8000) ,@pubdate datetime ,@category varchar(8000) ,@comments varchar(8000) ,@guid varchar(8000) ,@trackbackping varchar(8000) ,@ int out as set @ = 0 insert into channelsdetails ([channelid],[title],[description],[link],[pubdate],[category],[comments],[guid],[trackbackping]) select id,@title,@description,@link,@pubdate,@category,@comments,isnull(@guid,@link),@trackbackping from channels where not exists (select 1 from channelsdetails where guid = isnull(@guid,@link)) and url = @url select @ = scope_identity() go set quoted_identifier off go set ansi_nulls on go */