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 = "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}

时间: 2024-12-23 09:08:32

ASP.NET生成RSS Feed的相关文章

ASP.NET 生成 RSS Feed

asp.net|rss 前段时间在写RSS Feed. 经过了几次的修改,把相关的代码写成了单独的类.感觉重用时还算比较方便的.于是贴出来,大家一起研究研究.以下是RssBase.cs类:   1using System;  2using System.Collections.Generic;  3using System.Xml;  4  5namespace MyMedia.Utilities  6{  7    public class RssBase  8    {  9       

可以应用到马克斯电影站生成Rss Feed的代码_应用技巧

使用方法:将下面的代码存为rss.asp(记得以UTF-8格式保存)并上传到网站根目录,剩下的工作就是找Rss提交入口去提交你的Feed地址http://你的域名/rss.asp 演示:http://www.366mv.cn  代码如下,请根据注释修改相应的信息,版权信息还望各位能够保留 ^_^  复制代码 代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  <%  Session.CodePage=6

可以应用到马克斯电影站生成Rss Feed的代码

使用方法:将下面的代码存为rss.asp(记得以UTF-8格式保存)并上传到网站根目录,剩下的工作就是找Rss提交入口去提交你的Feed地址http://你的域名/rss.asp  演示:http://www.366mv.cn  代码如下,请根据注释修改相应的信息,版权信息还望各位能够保留 ^_^ 复制代码 代码如下: <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  <%  Session.CodePage=6

asp 动态生成rss(不成生xml文件)代码_应用技巧

rss的优点 1.您可以有选择地浏览您感兴趣的以及与您的工作相关的新闻. 2.您可以把需要的信息从不需要的信息(兜售信息,垃圾邮件等)中分离出来. 3.您可以创建自己的新闻频道,并将之发布到因特网. 什么是 RSS? RSS 指 Really Simple Syndication(真正简易联合) RSS 使您有能力聚合(syndicate)网站的内容 RSS 定义了非常简单的方法来共享和查看标题和内容 RSS 文件可被自动更新 RSS 允许为不同的网站进行视图的个性化 RSS 使用 XML 编写

asp 动态生成rss(不成生xml文件)代码

rss的优点 1.您可以有选择地浏览您感兴趣的以及与您的工作相关的新闻. 2.您可以把需要的信息从不需要的信息(兜售信息,垃圾邮件等)中分离出来. 3.您可以创建自己的新闻频道,并将之发布到因特网. 什么是 RSS? RSS 指 Really Simple Syndication(真正简易联合) RSS 使您有能力聚合(syndicate)网站的内容 RSS 定义了非常简单的方法来共享和查看标题和内容 RSS 文件可被自动更新 RSS 允许为不同的网站进行视图的个性化 RSS 使用 XML 编写

asp.net 页面中生成 RSS 2.0 提要

asp.net|rss|页面 Figure 1 Sample RSS 1.0 Document <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" > <channel rdf:about="

PHP RSS/Feed类库

通用PHP RSS/Feed 生成类库(支持RSS 1.0/2.0和ATOM) PHP Universal Feed Generator (supports RSS 1.0, RSS 2.0 and ATOM)   可 生成的RSS版本: RSS 1.0 (which officially obsoleted RSS 0.90) RSS 2.0 (which officially obsoleted RSS 0.91, 0.92, 0.93 and 0.94) ATOM 1.0 功能: 可生成R

解析dom树和生成rss

我们学院的网站做得很烂,上面的新闻不提供rss订阅,每次看新闻都要跑到网站上去,非常不方便. 我现在是就业负责人,万一上面有什么新的通知没有看到,那我的责任就大了.昨天,突然有一个想法, 就是自己为其实现一个rss feed. 首先我考虑的是用rsslib,但发现rss lib生成的xml,普通的rss订阅器不识别,没办法,只能自己想 办法解决,最后自己观察了一个rss的feed,了解了一下基本结构后自己实现了一个.主要有RssDocument .RssChannel.RssImage.RssI

php生成RSS订阅的方法

 本文实例讲述了php生成RSS订阅的方法.分享给大家供大家参考.具体分析如下: RSS(简易信息聚合,也叫聚合内容)是一种描述和同步网站内容的格式.RSS可以是以下三个解释的其中一个: Really Simple Syndication:RDF (Resource Description Framework) Site Summary: Rich Site Summary.但其实这三个解释都是指同一种Syndication的技术.RSS目前广泛用于网上新闻频道,blog和wiki.使用RSS订