C# 生成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(){} }// }//
2、ChannelCollection.cs 類
using System; namespace Utility.Rss { /// <summary> /// rssChannelCollection 的摘要說明。 /// </summary> public class ChannelCollection : System.Collections.CollectionBase { public Channel this[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 xNode in 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(); } }// }//
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() { } }// }//
“C#中解析Rss實現思路及通用類”應用的例項:
string strHtml = ""; string url = "http://rss.xinhuanet.com/rss/native.xml"; Utility.Rss.Feed feed = new Utility.Rss.Feed(url,DateTime.Parse(System.DateTime.Now.AddDays(-1).ToShortDateString())); feed.Read(); strHtml += "[記錄數目:"+feed.Channel.Items.Count+"]<br><br>"; for(int i=0;i<feed.Channel.Items.Count;i++) { // arr = feed.Channel.Items[i].title.Split(cSplit); strHtml +=" <a href="+feed.Channel.Items[i].link+" target=_blank><B>"+ feed.Channel.Items[i].title + "</B></a><br>"; strHtml +=" <font color=red>"+feed.Channel.Items[i].pubDate + "</font><br>"; strHtml +=" "+feed.Channel.Items[i].description + "<br>"; } Response.Write(strHtml);
相關文章
- C#操作SQL Server通用類C#SQLServer
- C#解讀RSSC#
- php生成rss訂閱PHP
- C#:資料庫SQL操作通用類C#資料庫SQL
- RSS生成工具/服務推薦
- C#讀取Rss功能函式C#函式
- 在執行時生成C# .NET類C#
- 【乾貨分享】C# 實體類生成工具C#
- c#官方推薦md5通用加密類C#加密
- C# RSS:新聞抓取正文並轉TXTC#
- RSS檔案輸出(ASP.NET C#版)ASP.NETC#
- Java通用DBHelper類Java
- Excel通用類工具(二)Excel
- elasticsearch通用工具類Elasticsearch
- python 爬取喜馬拉雅節目生成RSS FeedPython
- 利用 Laravel 花 2 小時擼一個 RSS 生成器Laravel
- 通用點陣字型檔生成工具
- java project打包生成jar包(通用)JavaProjectJAR
- c# 獲取電腦硬體資訊通用查詢類[測試通過]C#
- C#生成PDFC#
- C#根據雜湊演算法生成類似uuid字串C#演算法UI字串
- C# 生成縮圖C#
- openresty(nginx) redis 通用工具類RESTNginxRedis
- 使用 PubSubHubbub 製作 RSS 定時器 —— Laravel RSS (三)定時器Laravel
- 使用T4為資料庫自動生成實體類(C#)資料庫C#
- 使用FreeMarker/Jsp(WebWork2) 生成靜態/動態RSS檔案JSWeb
- nodejs生成多層目錄和生成檔案的通用方法NodeJS
- C#抽象類C#抽象
- C# 生成DLL 並 呼叫C#
- RSS 語法概述
- 瞭解下RSS 元素
- RSS概念和使用
- 通用mapper和分類實現APP
- Mybatis騷操作-通用查詢工具類MyBatis
- C#中介面、基類與類C#
- C#學習——基本類——Math類C#
- C# 繼承 子類(派生類) 父類(基類)C#繼承
- idunno.PasswordGenerator:生成類似1Password高熵密碼的C#庫熵密碼C#