/ * .NET / C #: Use reflection to write universal RSS 2.0 Reader
Recently I have written two types of code on a Simple Rss Reader: 1. Simple code, but not universal (eg some special RSS Reader) 2. The code is complex, but not enough time to digest (such as: rssbandit) )
自己 自己: Due to the basic attributes of RSS! But some special universal properties, such as: Slash: Comments wfw: comment wfw: Commentrss trackbackping does not necessarily exist! How to deal with it? Solution: 1. Class RssHeader is used to represent the header information of the RSS. You can add new properties to it, the principle is that the member variable fieild's name is the attribute name of the XML source corresponding to the attribute name, the XML property name contains ":": ":": " Filter it out! Such as:
2. Class Rssitem is used to indicate the principle of Item Item added to the new attribute with RSSheader!
3. After obtaining the XML source of the RSS, by recursive traversal node according to the actual RSS attribute according to the actual RSS attribute, "Constructivity" RSSHEADER and RSSITEM! Please refer to the Travel method of Class SimplersSreader!
4. Database (this article uses Microshaoft Data Access Application Block 3.1) Table: Channels ChannelsDetails (Working Table) Field Name and its data type strictly followed by the attribute name corresponding to the XML source of the RSS, the XML property name contains ":" Filter out! Store procedure: sp_addchannel sp_addchannelsdetails parameter name and its data type strictly follow the attribute name corresponding to the XML source of RSS, the XML property name contains ":" to filter it out!
Command line compilation: csc simplersreader.cs /r:c:/Windows/Microsoft.Net/framework/v1.1.4322/system.data.OracleClient.dll
All Code SimplersSreader.cs here download http://www.cnblogs.com/files/microshaoft/simplersSreader.rar
* / namespace microshaoft {using system.xml; using system.text; using system.collections; using system.text.regularexpressions;
All Code SimplersSreader.cs here download http://www.cnblogs.com/files/microshaoft/simplersSreader.rar
* / namespace microshaoft {using system.xml; using system.text; using system.collections; using system.text.regularexpressions;
Public class {// feed url public {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._ilaguage;}}
Public String generator {get {return this._generator;}}
Public string ttl {get {return this._ttl;}}
Public string copyright {get {returnid;}}
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 {returnimen_manageditor;}}
Public string url {get {return this._url;}}
Public string dclanguage {get {return this._dclanguage;}}
// private Field following values reflected by the class SimpleRssReader assigning private string _dclanguage; // dc: language private string _URL; private string _managingEditor; private string _lastBuildDate; private string _title; private string _description; private string _link; private string _language Private string _generator; private string _ttl; private string _copyright; private string _pubdate; private string _category;
} Public class rssitem {private} {private rssheader_Header
Public Rssheader Header {Get {Return this._Header;}}
Field // private class SimpleRssReader value will be reflected by assigning private string _title; private string _link; private string _description; private string _category; private string _author; private string _pubDate; private string _comments; private string _guid; private string _slashcomments; Private string_wfwcomment; private string _wfwcommentrs; private string _trackbackback; public string trackbackping {get {return.com;}
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 analysis processing completion event public delegate void RssHeaderReceiveEventHandler (SimpleRssReader Sender, RssHeader Header); public event RssHeaderReceiveEventHandler RssHeaderReceive;
// A certain RSSITEM parsing process After the event, the public delegate Void RssiteMReceEventHndler (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 {return}}}}
// Used to store all RSSItem Private Rssitem [] _rsSitems;
Public} {get {return this._rsithms;}}
Public void {xmlDocument xd = new xmldocument (); // If the efficiency is not high, WebRequest replacing xd.Load (URL); XMLNodelist XNL = Xd.selectNodes ("/ RSS / CHANNEL");
THIS._RS = New RssHeader (URL);
THIS._TRS = TypeOf (RssHeader); this._tri = typeof (Rssitem);
THIS._RSSITEMSAL = New ArrayList ();
Foreach (XMLNode XN IN XNL) {// Recursively traversed this.travel (xn, 0);}
IF (this._rssitems = new {this._rssitems = new = = 0; Foreach (Object O in this._rsSitemsal) {this._rssitems [i ] = (rsSitem) O;}}}
///
Foreach (xmlnode n in node.childnodes) {if (n.name! = "iTEM") {if (! b) // rss header header {// According to the actual existing properties of XML, the private member reflected as RSSHeader instance is used. Assignment FieldInfo Fi = this._trs.getfield ("_" n.Name.Replace (":", "), bindingflags.nonpublic | bindingflags.public); if (fi! = Null) {FI .SetValue (this._rs, n.innertext);}} Else //}}} Else // RSS item {// According to the actual existing properties of XML, the private member reflected to the RSSITEM instance FieldInfo Fi = this._tri.Getfield ("_" n.Name.Replace (":", "), bindingflags.nonpublic | bindingflags.instance | bindingflags.public); if (fi! = null) {FI.SetValue (ri, n.innertext);}}} } If (! B) {// Trigger the RSSHeaderReceive event if (this.rsheaderRece! = Null) {this.rsheaderReceive (this, this._rs); }}} Else {// HEADER / Header FieldInfo Fi = this._tri.getfield ("_ header", bindingflags.nonpublic | bindingflags.instance | bindingflags.public; if (fi! = Null); if (fi! = Null); {FI.SetValue (ri, this._rs);
// Trigger the RSSITEMRECEVE event if (this.rsSiteMreceive! = Null) {this.rsSitemReceive (this, ri);} this._rstitemsal.add (ri);}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} (x, i);}}}}}}}}}}}}}}}
Public class util {public static datetime parsedatetime (string s) {datetime dt; if (s == null || s.tostring (). Length <= 0) {dt = DATETIME.NOW;} else {TRY {DT = DATETIME .Parse (s);} catCh {dt = DateTime.now;}} Return Dt;} ///