RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式。RSS可以是以下三个解释的其中一个: Really Simple
Syndication;RDF (Resource Description Framework) Site Summary; Rich Site
Summary。但其实这三个解释都是指同一种Syndication的技术。RSS目前广泛用于网上新闻频道,blog和wiki,主要的版本有0.91, 1.0,
2.0。使用RSS订阅能更快地获取信息,网站提供RSS输出,有利于让用户获取网站内容的最新更新。网络用户可以在客户端借助于支持RSS的聚合工具软件,在不打开网站内容页面的情况下阅读支持RSS输出的网站内容。
using System; namespace RssOperator { /**/ /// <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 { if (_title.Length > 21) { return _title.Substring(0, 21) + "..."; } else { 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 { if (value != null) { _description = value; } else { _description = ""; } } } /**/ /// <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(); } private string GetDate(DateTime dt) { string House = dt.ToString("HH"); int hour = int.Parse(House); int mine = dt.Minute; int second = dt.Second; string AP = ""; if (hour > 12) { AP = "下午"; hour = hour - 12; } else { AP = "上午"; } return string.Format("{0} {1} {2}", dt.ToString("yyyy/MM/dd"), AP, hour + ":" + mine + ":" + second); } public string DateString { get { DateTime time = Convert.ToDateTime(pubDate); return GetDate(time); } } public DateTime PuDate { get { return Convert.ToDateTime(pubDate); } } } }
using System.Collections.Generic; namespace RssOperator { public class Channel { private string _title; private string _link; private string _description; private List<Item> items = new List<Item>(); #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 List <Item > Items { get { return items; } } #endregion public Channel(){} }// }//
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Net; using System.IO; using System.Collections; using System.Collections.ObjectModel; namespace RssOperator { public class Operator { public Operator(Uri url) { this.Url = url; } private DateTime _BeginDate = new DateTime(1900, 1, 1); private DateTime _EndDate = DateTime.Now; private Uri _Url = new Uri("http://news.163.com/special/00011K6L/rss_gn.xml", UriKind.RelativeOrAbsolute); public DateTime BeginData { get { return _BeginDate; } set { _BeginDate = value; } } public DateTime EndData { get { return _EndDate; } set { _EndDate = value; } } public Uri Url { get { return _Url; } set { _Url = value; } } public Operator(Uri url, DateTime begindate, DateTime enddate) { this.BeginData = begindate; this.EndData = enddate; this.Url = url; } public Channel GetRssData() { Channel channel = new Channel(); 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)"; HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr; //System.Xml.XmlReader = new XmlReader(); //stream=Encoding.Convert(Encoding.GetEncoding("GBK"),Encoding.GetEncoding("gb2312"),Convert.ToSByte(stream)); if (response.Headers["Content-Type"].ToString() == "GBK") { sr = new StreamReader(stream, System.Text.Encoding.GetEncoding("GB2312")); xDoc.Load(sr); } else { xDoc.Load(stream); } if (xDoc != null) { var items = xDoc.DocumentElement["channel"].SelectNodes("item"); channel.title = xDoc.DocumentElement["channel"].SelectSingleNode("title").InnerText; channel.link = xDoc.DocumentElement["channel"].SelectSingleNode("link").InnerText; channel.description = xDoc.DocumentElement["channel"].SelectSingleNode("description").InnerText; foreach (XmlNode item in items) { Item rt = new Item(); rt.title = item.SelectSingleNode("title").InnerText.Replace("'", "''"); rt.link = item.SelectSingleNode("link").InnerText.Replace("'", "''"); try { rt.description = item.SelectSingleNode("description").InnerText.Replace("'", "''"); } catch { } try { rt.pubDate = item.SelectSingleNode("pubDate").InnerText; } catch { } channel.Items.Add(rt); } } return channel; } public void CreateRssData(Channel c) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.AppendLine( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type=\"text/xsl\" href=\"http://163.dynamic.feedsportal.com/xsl/eng/rss.xsl\" "); stringBuilder.AppendLine( " <rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" "); stringBuilder.AppendLine( " xmlns:taxo=\"http://purl.org/rss/1.0/modules/taxonomy/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" version=\"2.0\">"); stringBuilder.AppendLine(@"<channel><title>" + c.title + "</title><link>" + c.link + "</link><description>" + c.description + "</description>"); foreach (var item in c.Items) { stringBuilder.AppendLine(String.Format(@"<title>{0}</title>", item.title)); stringBuilder.AppendLine(String.Format(@"<link>{0}</link>", item.link)); stringBuilder.AppendLine(String.Format(@"<description>{0}</description>", item.description)); stringBuilder.AppendLine(String.Format(@"<pubDate>{0}</pubDate>", item.pubDate)); } stringBuilder.AppendLine("</channel></rss>"); FileStream fs = new FileStream(DateTime.Now.ToString("yyyyMMddhhssmm") + ".xml", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(stringBuilder.ToString()); } } }