国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Web 2.0時代RSS的.Net實現

2019-11-18 17:09:48
字體:
來源:轉載
供稿:網友

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 = @" 15        const string slashNamespaceUri = @" 16        const string syndicationNamespaceUri = @" 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  AddRss
PReamble( 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信息了

http://jillzhang.VEVb.com/archive/2006/06/11/423086.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新绛县| 红安县| 易门县| 泸定县| 济宁市| 河津市| 乳山市| 靖远县| 通州区| 常熟市| 利川市| 乡城县| 页游| 武清区| 砚山县| 双鸭山市| 太湖县| 广河县| 周宁县| 万山特区| 阳春市| 廊坊市| 肥西县| 晴隆县| 天峨县| 梁山县| 虞城县| 肇庆市| 沂南县| 新巴尔虎左旗| 墨竹工卡县| 偏关县| 迁安市| 玛多县| 正宁县| 滁州市| 宁都县| 滨海县| 冕宁县| 墨玉县| 顺义区|