首页 > 编程 > .NET > 正文

ASP.NET 生成 RSS Feed

2024-07-10 13:09:41
字体:
来源:转载
供稿:网友

  前段时间在写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 = "http://www.mywii.com.au";
153        private const string xmlns_dc = "http://purl.org/dc/elements/1.1/";
154        private const string link = "http://www.mywii.com.au/";
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 = "http://www.mywii.com.au/img/main/feed.gif";
160        private const string image_title = "mywii";
161        private const string image_link = "http://www.mywii.com.au";
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 :)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表