首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 认证考试-> 微软认证
web 2.0时代rss的.net实现_asp.net技巧
作者:网友供稿 点击:0
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 

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信息了

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


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·学习java需要知道的一些问题-JSP教程,Java技巧及代码
·vs.net中web services入门-.NET教程,Web Service开发
·C#中Base64之编码,解码方法-.NET教程,C#语言
·关于程序加载错误的处理-ASP教程,ASP应用
·.Net应用程序发布问题的最新解决方案,感觉比较爽(可桌面、程序中加自己的ICO及卸载等)-.NET教程,评论及其它
·设计模式-简单工厂模式(SimpleFactory-C#)-.NET教程,C#语言
·用photoshop制作logo-网页设计,Photoshop
·用jsp实现直接下载文件而不是在浏览器中打开的功能-JSP教程,Jsp/Servlet
·利用数据集实现对数据库的操作-.NET教程,数据库应用
·JAVA与数据库连接方法(二)-JSP教程,数据库相关
最新文章
·当windows vista系统提示“内存不足”怎么办?_windows vista
·王通:个人如何利用网络赚钱(1)_网赚技巧
·关于flash中注册点与中心点的区别_flash教程
·个人网站发展初期如何节省资金_站长心得
·如何写好“帮助中心”的内容_站长心得
·中国个人网站——新经济中的非主流2_站长心得
·backpack - 体验可读写的web服务_站长心得
·中文搜索引擎的研究_站长心得
·域名选取十技巧_站长心得
·用javascript 转换外部链接样式_javascript教程
相关主题
  • web 2.0 网站成功的关键2_站长心得
  • web 2.0 网站成功的关键1_站长心得
  • Web 2.0时代RSS的.Net实现-.NET教程,评论及其它
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭