位置:海鸟网 > IT > ASP.NET >

ASP.NET 生成 RSS Feed

前段时间在写RSS Feed。

  经过了几次的修改,把相关的代码写成了单独的类。感觉重用时还算比较方便的。于是贴出来,大家一起研究研究。以下是RssBase.cs类:

  1using System;
  2using System.Collections.Generic;
  3using System.Xml;
  4
  5namespace MyMedia.Utilities
  6{
  7    public class RssBase
  8    {
  9        RssBase constructor#region RssBase constructor
 10        public RssBase(XmlTextWriter rssWriter)
 11        {
 12            this.rssWriter = rssWriter;
 13        }
 14        public RssBase(XmlTextWriter rssWriter, bool hasMedia, IList<ItemInfo> items)
 15        {
 16            this.rssWriter = rssWriter;
 17            this.hasMedia = hasMedia;
 18            this.items = items;
 19        }
 20        #endregion
 21
 22        [Serializable]
 23        public sealed class ItemInfo
 24        {
 25            ItemInfo constructor#region ItemInfo constructor
 26            public ItemInfo() { }
 27            public ItemInfo(string itemTitle, string itemLink, string itemDescription, string itemPubDate, string itemAuthor, string itemGuid)
 28            {
 29                this.itemTitle = itemTitle;
 30                this.itemLink = itemLink;
 31                this.itemDescription = itemDescription;
 32                this.itemAuthor = itemAuthor;
 33                this.itemGuid = itemGuid;
 34                this.itemPubDate = itemPubDate;
 35            }
 36            public ItemInfo(string itemTitle, string itemLink, string itemDescription, string itemPubDate, string itemAuthor,
 37                            string itemGuid, string mediaContentUrl, string mediaTitle, string mediaDescription,
 38                            string mediaThumbUrl, string mediaThumbWidth, string mediaThumbHeight, string mediaAuthor)
 39            {
 40                this.itemTitle = itemTitle;
 41                this.itemLink = itemLink;
 42                this.itemDescription = itemDescription;
 43                this.itemPubDate = itemPubDate;
 44                this.itemAuthor = itemAuthor;
 45                this.itemGuid = itemGuid;
 46                this.mediaContentUrl = mediaContentUrl;
 47                this.mediaTitle = mediaTitle;
 48                this.mediaDescription = mediaDescription;
 49                this.mediaThumbUrl = mediaThumbUrl;
 50                this.mediaThumbWidth = mediaThumbWidth;
 51                this.mediaThumbHeight = mediaThumbHeight;
 52                this.mediaAuthor = mediaAuthor;
 53            }
 54            #endregion
 55
 56            Internal item variables#region Internal item variables
 57            // Item variables
 58            private string itemTitle;
 59            private string itemLink;
 60            private string itemDescription;
 61            private string itemPubDate = DateTime.Now.ToString("r");
 62            private string itemDateTaken = DateTime.Now.ToString();
 63            private string itemAuthor;
 64            private string itemGuid;
 65            // Media variables
 66            private string mediaContentUrl;
 67            private string mediaTitle;
 68            private string mediaDescription;
 69            private string mediaThumbUrl;
 70            private string mediaThumbWidth;
 71            private string mediaThumbHeight;
 72            private string mediaAuthor;
 73            #endregion
 74
 75            Item properties#region Item properties
 76            public string ItemTitle
 77            {
 78                set { itemTitle = value; }
 79                get { return itemTitle; }
 80            }
 81            public string ItemLink
 82            {
 83                set { itemLink = value; }
 84                get { return itemLink; }
 85            }
 86            public string ItemDescription
 87            {
 88                set { itemDescription = value; }
 89                get { return itemDescription; }
 90            }
 91            public string ItemPubDate
 92            {
 93                set { itemPubDate = value; }
 94                get { return itemPubDate; }
 95            }
 96            public string ItemDateTaken
 97            {
 98                set { itemDateTaken = value; }
 99                get { return itemDateTaken; }
100            }
101            public string ItemAuthor
102            {
103                set { itemAuthor = value; }
104                get { return itemAuthor; }
105            }
106            public string ItemGuid
107            {
108                set { itemGuid = value; }
109                get { return itemGuid; }
110            }
111            public string MediaContentUrl
112            {
113                set { mediaContentUrl = value; }
114                get { return mediaContentUrl; }
115            }
116            public string MediaTitle
117            {
118                set { mediaTitle = value; }
119                get { return mediaTitle; }
120            }
121            public string MediaDescription
122            {
123                set { mediaDescription = value; }
124                get { return mediaDescription; }
125            }
126            public string MediaThumbUrl
127            {
128                set { mediaThumbUrl = value; }
129                get { return mediaThumbUrl; }
130            }
131            public string MediaThumbWidth
132            {
133                set { mediaThumbWidth = value; }
134                get { return mediaThumbWidth; }
135            }
136            public string MediaThumbHeight
137            {
138                set { mediaThumbHeight = value; }
139                get { return mediaThumbHeight; }
140            }
141            public string MediaAuthor
142            {
143                set { mediaAuthor = value; }
144                get { return mediaAuthor; }
145            }
146            #endregion
147
148        }
149
150        Const string#region Const string
151        private const string TITLE = "MyMedia RSS";
152        private const string XMLNS_MEDIA = "";
153        private const string XMLNS_DC = "";
154        private const string LINK = "";
155        private const string DESCRIPTION = "description";
156        private const string LANGUAGE = "en-us";
157        private const string COPYRIGHT = "Copyright 2006";
158        private const string GENERATOR = "MyMedia RSS Generator";
159        private const string IMAGE_URL = "";
160        private const string IMAGE_TITLE = "MyWii";
161        private const string IMAGE_LINK = "";
162        private string PUB_DATE = DateTime.Now.ToString("r");
163        private string LAST_BUILD_DATE = DateTime.Now.ToString("r");
164
165        #endregion
166
167        Internal member variables#region Internal member variables
168        private XmlTextWriter rssWriter;
169        private bool hasMedia;
170        private IList<ItemInfo> items;
171        #endregion
172
173        Properties#region Properties
174        public XmlTextWriter RssWriter
175        {
176            set { rssWriter = value; }
177            get { return rssWriter; }
178        }
179        public bool HasMedia
180        {
181            set { hasMedia = value; }
182            get { return hasMedia; }
183        }
184        public IList<ItemInfo> Items
185        {
186            set { items = value; }
187            get { return items; }
188        }
189
190        #endregion
191
192        Private method#region Private method
193
194        private void WritePrologue()
195        {
196            rssWriter.WriteStartDocument();
197            rssWriter.WriteStartElement("rss");
198            rssWriter.WriteAttributeString("version", "2.0");
199            rssWriter.WriteAttributeString("xmlns:media", XMLNS_MEDIA);
200            rssWriter.WriteAttributeString("xmlns:dc", XMLNS_DC);
201            rssWriter.WriteString("\n    ");
202            // Write in channel block
203            rssWriter.WriteStartElement("channel");
204            rssWriter.WriteString("\n     ");
205            rssWriter.WriteElementString("title", TITLE);
206            rssWriter.WriteString("\n     ");
207            rssWriter.WriteElementString("link", LINK);
208            rssWriter.WriteString("\n     ");
209            rssWriter.WriteElementString("description", DESCRIPTION);
210            rssWriter.WriteString("\n     ");
211            rssWriter.WriteElementString("pubDate", PUB_DATE);
212            rssWriter.WriteString("\n     ");
213            rssWriter.WriteElementString("lastBuildDate", LAST_BUILD_DATE);
214            rssWriter.WriteString("\n     ");
215            rssWriter.WriteElementString("language", LANGUAGE);
216            rssWriter.WriteString("\n     ");
217            rssWriter.WriteElementString("copyright", COPYRIGHT);
218            rssWriter.WriteString("\n     ");
219            rssWriter.WriteElementString("generator", GENERATOR);
220            rssWriter.WriteString("\n     ");
221            // Write in image block
222            rssWriter.WriteStartElement("image");
223            rssWriter.WriteString("\n        ");
224            rssWriter.WriteElementString("url", IMAGE_URL);
225            rssWriter.WriteString("\n        ");
226            rssWriter.WriteElementString("title", IMAGE_TITLE);
227            rssWriter.WriteString("\n        ");
228            rssWriter.WriteElementString("link", IMAGE_LINK);
229            rssWriter.WriteString("\n     ");
230            rssWriter.WriteEndElement();
231            rssWriter.WriteString("\n     ");
232        }
233
234        /**//// <summary>
235        /// Write in items
236        /// </summary>
237        private void WriteItem(ItemInfo item)
238        {
239            // Write in item block
240            rssWriter.WriteStartElement("item");
241            rssWriter.WriteString("\n        ");
242            rssWriter.WriteElementString("title", item.ItemTitle);
243            rssWriter.WriteString("\n        ");
244            rssWriter.WriteElementString("link", item.ItemLink);
245            rssWriter.WriteString("\n        ");
246
247            rssWriter.WriteStartElement("description");
248            rssWriter.WriteString("\n         ");
249            rssWriter.WriteCData(item.ItemDescription);
250            rssWriter.WriteString("\n        ");
251            rssWriter.WriteEndElement();
252
253            rssWriter.WriteString("\n        ");
254            rssWriter.WriteElementString("pubDate", item.ItemPubDate);
255            rssWriter.WriteString("\n        ");
256            rssWriter.WriteElementString("dc:date.Taken", item.ItemDateTaken);
257            rssWriter.WriteString("\n        ");
258            rssWriter.WriteElementString("author", item.ItemAuthor);
259            rssWriter.WriteString("\n        ");
260            rssWriter.WriteStartElement("guid");
261            rssWriter.WriteAttributeString("isPermaLink", "false");
262            rssWriter.WriteString(item.ItemGuid);
263            rssWriter.WriteEndElement();
264
265
266            // Write item media here
267
268        }
269
270        /**//// <summary>
271        /// Write in item media
272        /// </summary>
273        /// <param name="item">ItemInfo item</param>
274        private void WriteItemMedia(ItemInfo item)
275        {
276            // Write in media
277            rssWriter.WriteString("\n        ");
278            rssWriter.WriteStartElement("media:content");
279            rssWriter.WriteAttributeString("url", item.MediaContentUrl);
280            rssWriter.WriteAttributeString("type", "image/jpeg");
281            rssWriter.WriteEndElement();
282            rssWriter.WriteString("\n        ");
283            rssWriter.WriteElementString("media:title", item.MediaTitle);
284            rssWriter.WriteString("\n        ");
285            rssWriter.WriteStartElement("media:text");
286            rssWriter.WriteAttributeString("type", "html");
287            rssWriter.WriteString(item.MediaDescription);
288            rssWriter.WriteEndElement();
289            rssWriter.WriteString("\n        ");
290            rssWriter.WriteStartElement("media:thumbnail");
291            rssWriter.WriteAttributeString("url", item.MediaThumbUrl);
292            rssWriter.WriteAttributeString("height", item.MediaThumbHeight);
293            rssWriter.WriteAttributeString("width", item.MediaThumbWidth);
294            rssWriter.WriteEndElement();
295            rssWriter.WriteString("\n        ");
296            rssWriter.WriteStartElement("media:credit");
297            rssWriter.WriteAttributeString("role", "photographer");
298            rssWriter.WriteString(item.MediaAuthor);
299            rssWriter.WriteEndElement();
300            rssWriter.WriteString("\n     ");
301
302        }
303
304        /**//// <summary>
305        /// Close item element
306        /// </summary>
307        private void WriteItemEnd()
308        {
309            rssWriter.WriteString("\n     ");
310            rssWriter.WriteEndElement();
311            rssWriter.WriteString("\n     ");
312        }
313
314        /**//// <summary>
315        /// Close all the elements
316        /// </summary>
317        private void WriteEnd()
318        {
319            rssWriter.WriteEndElement();
320            rssWriter.WriteString("\n");
321            rssWriter.WriteEndElement();
322            rssWriter.WriteEndDocument();
323        }
324
325        #endregion
326
327        /**//// <summary>
328        /// Write RSS feed
329        /// </summary>
330        public void WriteRSS()
331        {
332            WritePrologue();
333            foreach (ItemInfo info in items)
334            {
335                WriteItem(info);
336                if (hasMedia)
337                    WriteItemMedia(info);
338                WriteItemEnd();
339            }
340            WriteEnd();
341        }
342    }
343}

  然后使用一个FeedBase.cs类,调用上面的RssBase类中的方法和属性。代码如下。(该文件有改动,如要编译,需做相关修改)

1 using System.Collections.Generic;
2 using System.Configuration;
3 using System.Data;
4 using System.Text;
5 using System.Web;
6 using System.Web.UI;
7 using System.Xml;
8
9
10public class FeedBase : Page
11{
12    public void GenerateRss()
13    {
14
15        IList<RssBase.ItemInfo> items = new List<RssBase.ItemInfo>();
16
17        // Populate items.
18        foreach (DataRow row in rows)
19        {
20            RssBase.ItemInfo item = new RssBase.ItemInfo(row["Title"].ToString(), "link", row["Description"].ToString(), row["PubDate"].ToString(), row["Author"].ToString(), "guid");
21            items.Add(item);
22        }
23
24        XmlTextWriter xmlWriter = new XmlTextWriter(Response.OutputStream,
25            Encoding.UTF8);
26
27        RssBase rssBase = new RssBase(xmlWriter, false, items);
28        rssBase.WriteRSS();
29
30        xmlWriter.Flush();
31        xmlWriter.Close();
32
33        Response.ContentEncoding = Encoding.UTF8;
34        Response.ContentType = "text/xml";
35        Response.Cache.SetCacheability(HttpCacheability.Public);
36
37        Response.End();
38    }
39
40 }

  最后,只要新建一个feed.aspx文件,继承FeedBase类,调用该类中的GenerateRss()方法就可以轻松生成你所期望的RSS Feed :)