设为首页
加入收藏
站内地图
旧版入口
当前位置:首页 > 站长学院 > 网络编程 > ASP.NET

Web 2.0时代RSS的.Net实现

作者:佚名 出处:网络转载 时间:06-12 点击:

内容载入中...
版权声明: 华腾联合

Web2.0时是以Blog,Wike,Tag,RSS等技术为代表的以个性化为中心的新一代互联网模式,RSS比起Blog等名词似乎还不算太热。但打开网页仍是遍布了RSS,XML等醒目的图标,打开页面Mathon浏览器也是一个劲的提示有新的RSS连接,前一段一个项

目需要,自己写了一个.Net下面生成RSS信息的类,如下:


  1using System;
  2using System.XML;
  3using System.Collections;
  4using System.Globalization;
  5using System.Web;
  6
  7namespace BLRL
  8{
  9    /// <summary>
 10    /// Summary description for Rss.
 11    /// </summary>
 12    public class Rss
 13    {
 14        const string dublinCoreNamespaceUri = @"http://purl.org/dc/elements/1.1/";
 15        const string slashNamespaceUri = @"http://purl.org/rss/1.0/modules/slash/";
 16        const string syndicationNamespaceUri = @"http://purl.org/rss/1.0/modules/syndication/";
 17        //RSS频道结构
 18        struct RssChannel 
 19        {
 20            public string title;//标题
 21            public string link;//连接
 22            public string language;//语言            
 23            public string description;//描述
 24            public string webMaster;//发布者
 25        }
 26
 27        //RSS图片信息
 28        struct RssImage
 29        {
 30            public string url;//地址
 31            public string title;//标题
 32            public int height ;//高度
 33            public int width;//长度
 34        }
 35        
 36        //RSS项结构
 37        struct RssItem 
 38        {
 39            public string title;//标题
 40            public string catalog;//类别
 41            public string link;//连接
 42            public DateTime pubDate;//发布日期
 43            public string description;//描述
 44
 45        }
 46        public Rss()
 47        {
 48            //
 49            // TODO: Add constructor logic here
 50            //
 51        }
 52        /// <summary>
 53        ///添加rss版本信息
 54        /// </summary>
 55        /// <param name="XMLDocument"></param>
 56        /// <returns></returns>
 57        public static XMLDocument  AddRssPreamble( XMLDocument XMLDocument)
 58        {
 59            //声明创建1.0版本得XML
 60            XMLDeclaration XMLDeclaration = XMLDocument.CreateXMLDeclaration("1.0", "utf-8", null);
 61            XMLDocument.InsertBefore(XMLDeclaration, XMLDocument.DocumentElement);
 62
 63            XMLElement rssElement = XMLDocument.CreateElement("rss");
 64
 65            XMLAttribute rssVersionAttribute = XMLDocument.CreateAttribute("version");
 66            rssVersionAttribute.InnerText = "2.0";
 67            rssElement.Attributes.Append(rssVersionAttribute);
 68            XMLDocument.AppendChild(rssElement);
 69
 70           
 71            XMLAttribute dublicCoreNamespaceUriAttribute = XMLDocument.CreateAttribute("XMLns:dc");
 72            dublicCoreNamespaceUriAttribute.InnerText = dublinCoreNamespaceUri;
 73            rssElement.Attributes.Append(dublicCoreNamespaceUriAttribute);
 74
 75            XMLAttribute slashNamespaceUriAttribute = XMLDocument.CreateAttribute("XMLns:slash");
 76            slashNamespaceUriAttribute.InnerText = slashNamespaceUri;
 77            rssElement.Attributes.Append(slashNamespaceUriAttribute);
 78
 79            XMLAttribute syndicationNamespaceUriAttribute = XMLDocument.CreateAttribute("XMLns:sy");
 80            syndicationNamespaceUriAttribute.InnerText = syndicationNamespaceUri;
 81            rssElement.Attributes.Append(syndicationNamespaceUriAttribute);
 82
 83
 84            return XMLDocument;
 85        }
 86        
 87        /// <summary>
 88        /// 添加频道
 89        /// </summary>
 90        /// <param name="XMLDocument"></param>
 91        /// <param name="channel"></param>
 92        /// <returns></returns>
 93        private static XMLDocument AddRssChannel( XMLDocument XMLDocument, RssChannel channel) 
 94        {
 95            XMLElement channelElement = XMLDocument.CreateElement("channel");
 96            XMLNode rssElement = XMLDocument.SelectSingleNode("rss");
 97
 98            rssElement.AppendChild(channelElement);
 99
100            //添加标题
101            XMLElement channelTitleElement = XMLDocument.CreateElement("title");
102            channelTitleElement.InnerText = channel.title;
103            channelElement.AppendChild(channelTitleElement);
104
105            //添加连接
106            XMLElement channelLinkElement = XMLDocument.CreateElement("link");
107            channelLinkElement.InnerText = channel.link;
108            channelElement.AppendChild(channelLinkElement);
109
110            //添加描述
111            XMLElement channelDescriptionElement = XMLDocument.CreateElement("description");
112            XMLCDataSection cDataDescriptionSection = XMLDocument.CreateCDataSection(channel.description);
113            channelDescriptionElement.AppendChild(cDataDescriptionSection);
114            channelElement.AppendChild(channelDescriptionElement);
115            
116            //添加语言
117            XMLElement languageElement = XMLDocument.CreateElement("language");
118            languageElement.InnerText = channel.language;
119            channelElement.AppendChild(languageElement);
120
121            //添加发布者
122            XMLElement webMasterElement = XMLDocument.CreateElement("webMaster");
123            webMasterElement.InnerText = channel.webMaster;
124            channelElement.AppendChild(webMasterElement);
125
126            return XMLDocument;
127        }
128
129
130        //添加RssImage
131        private static XMLDocument AddRssImage(XMLDocument XMLDocument, RssImage img) 
132        {
133            XMLElement imgElement = XMLDocument.CreateElement("image");
134            XMLNode channelElement = XMLDocument.SelectSingleNode("rss/channel");  
135
136            //创建标题
137            XMLElement imageTitleElement = XMLDocument.CreateElement("title");
138            imageTitleElement.InnerText = img.title;
139            imgElement.AppendChild(imageTitleElement);
140
141            //创建地址
142            XMLElement imageUrlElement = XMLDocument.CreateElement("url");
143            imageUrlElement.InnerText = img.url;
144            imgElement.AppendChild(imageUrlElement);
145
146            //创建高度
147            XMLElement imageHeightElement = XMLDocument.CreateElement("height");
148            imageHeightElement.InnerText = img.height.ToString();
149            imgElement.AppendChild(imageHeightElement);
150
151            //创建长度
152            XMLElement imageWidthElement = XMLDocument.CreateElement("width");
153            imageWidthElement.InnerText = img.width.ToString();
154            imgElement.AppendChild(imageWidthElement);
155
156            //将图像节点添加到频道节点里面
157            channelElement.AppendChild(imgElement);
158            return XMLDocument;
159          
160        }
161
162
163        /// <summary>
164        /// 添加项信息
165        /// </summary>
166        /// <param name="XMLDocument"></param>
167        /// <param name="item"></param>
168        /// <returns></returns>
169        private static XMLDocument AddRssItem (XMLDocument XMLDocument, RssItem item) 
170        {
171            XMLElement itemElement = XMLDocument.CreateElement("item");
172            XMLNode channelElement = XMLDocument.SelectSingleNode("rss/channel");
173
174            //创建标题
175            XMLElement itemTitleElement = XMLDocument.CreateElement("title");            
176            XMLCDataSection cDataTitleSection = XMLDocument.CreateCDataSection(item.title);
177            itemTitleElement.AppendChild(cDataTitleSection);            
178            itemElement.AppendChild(itemTitleElement);
179
180            //创建日期
181            XMLElement pubDateElement = XMLDocument.CreateElement("pubDate");
182            pubDateElement.InnerText = XMLConvert.ToString(item.pubDate.ToUniversalTime(), "yyyy-MM-ddTHH:mm:ss");
183            itemElement.AppendChild(pubDateElement);
184            
185            //添加连接
186            XMLElement itemLinkElement = XMLDocument.CreateElement("link");
187            itemLinkElement.InnerText = item.link;
188            itemElement.AppendChild(itemLinkElement);
189
190            //创建描述
191            XMLElement itemDescriptionElement = XMLDocument.CreateElement("description");
192            XMLCDataSection cDataDescriptionSection = XMLDocument.CreateCDataSection(item.description);            
193            itemDescriptionElement.AppendChild(cDataDescriptionSection);
194            itemElement.AppendChild(itemDescriptionElement);
195
196
197            //创建类型
198            XMLElement itemcatalogElement = XMLDocument.CreateElement("catalog");
199            itemcatalogElement.InnerText = item.catalog;
200            itemElement.AppendChild(itemcatalogElement);
201
202            //将RssItem添加到频道节点里面
203            channelElement.AppendChild(itemElement);
204
205            return XMLDocument;
206        }
207   }
208}

根据特定的需要,可以先将数据读取到列表里面,然后遍历列表,调用上述方法,生成XML字符串。
这个字符串就是RS用到XML字符串了。也可以入ASPx文件,然后用
<link type="application/rss+XML" rel="alternate" href="rssfeed.ASPx">
调用下RSS文件,马桶等软件就会自动提示有RRS信息了
收藏本文:
】【打印页面】【推荐给朋友】【关闭窗口

站长学院

推荐信息