技术开发 频道

Web 2.0时代RSS的.Net实现

    【IT168 技术文档】Web.时是以Blog,Wike,Tag,RSS等技术为代表的以个性化为中心的新一代互联网模式,RSS比起Blog等名词似乎还不算太热。但打开网页仍是遍布了RSS,Xml等醒目的图标,打开页面Mathon浏览器也是一个劲的提示有新的RSS连接,前一段一个项目需要,自己写了一个.Net下面生成RSS信息的类,如下:

using System; using System.Xml; using System.Collections; using System.Globalization; using System.Web; namespace BLRL { /// <summary> /// Summary description for Rss. /// </summary> public class Rss { const string dublinCoreNamespaceUri = @"http://purl.org/dc/elements/./"; const string slashNamespaceUri = @"http://purl.org/rss/./modules/slash/"; const string syndicationNamespaceUri = @"http://purl.org/rss/./modules/syndication/"; //RSS频道结构 struct RssChannel { public string title;//标题 public string link;//连接 public string language;//语言 public string description;//描述 public string webMaster;//发布者 } //RSS图片信息 struct RssImage { public string url;//地址 public string title;//标题 public int height ;//高度 public int width;//长度 } //RSS项结构 struct RssItem { public string title;//标题 public string catalog;//类别 public string link;//连接 public DateTime pubDate;//发布日期 public string description;//描述 } public Rss() { // // TODO: Add constructor logic here // } /// <summary> ///添加rss版本信息 /// </summary> /// <param name="xmlDocument"></param> /// <returns></returns> public static XmlDocumentAddRssPreamble( XmlDocument xmlDocument) { //声明创建.版本得xml XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration(".", "utf-", null); xmlDocument.InsertBefore(xmlDeclaration, xmlDocument.DocumentElement); XmlElement rssElement = xmlDocument.CreateElement("rss"); XmlAttribute rssVersionAttribute = xmlDocument.CreateAttribute("version"); rssVersionAttribute.InnerText = "."; rssElement.Attributes.Append(rssVersionAttribute); xmlDocument.AppendChild(rssElement); XmlAttribute dublicCoreNamespaceUriAttribute = xmlDocument.CreateAttribute("xmlns:dc"); dublicCoreNamespaceUriAttribute.InnerText = dublinCoreNamespaceUri; rssElement.Attributes.Append(dublicCoreNamespaceUriAttribute); XmlAttribute slashNamespaceUriAttribute = xmlDocument.CreateAttribute("xmlns:slash"); slashNamespaceUriAttribute.InnerText = slashNamespaceUri; rssElement.Attributes.Append(slashNamespaceUriAttribute); XmlAttribute syndicationNamespaceUriAttribute = xmlDocument.CreateAttribute("xmlns:sy"); syndicationNamespaceUriAttribute.InnerText = syndicationNamespaceUri; rssElement.Attributes.Append(syndicationNamespaceUriAttribute); return xmlDocument; } /// <summary> /// 添加频道 /// </summary> /// <param name="xmlDocument"></param> /// <param name="channel"></param> /// <returns></returns> private static XmlDocument AddRssChannel( XmlDocument xmlDocument, RssChannel channel) { XmlElement channelElement = xmlDocument.CreateElement("channel"); XmlNode rssElement = xmlDocument.SelectSingleNode("rss"); rssElement.AppendChild(channelElement); //添加标题 XmlElement channelTitleElement = xmlDocument.CreateElement("title"); channelTitleElement.InnerText = channel.title; channelElement.AppendChild(channelTitleElement); //添加连接 XmlElement channelLinkElement = xmlDocument.CreateElement("link"); channelLinkElement.InnerText = channel.link; channelElement.AppendChild(channelLinkElement); //添加描述 XmlElement channelDescriptionElement = xmlDocument.CreateElement("description"); XmlCDataSection cDataDescriptionSection = xmlDocument.CreateCDataSection(channel.description); channelDescriptionElement.AppendChild(cDataDescriptionSection); channelElement.AppendChild(channelDescriptionElement); //添加语言 XmlElement languageElement = xmlDocument.CreateElement("language"); languageElement.InnerText = channel.language; channelElement.AppendChild(languageElement); //添加发布者 XmlElement webMasterElement = xmlDocument.CreateElement("webMaster"); webMasterElement.InnerText = channel.webMaster; channelElement.AppendChild(webMasterElement); return xmlDocument; } //添加RssImage private static XmlDocument AddRssImage(XmlDocument xmlDocument, RssImage img) { XmlElement imgElement = xmlDocument.CreateElement("image"); XmlNode channelElement = xmlDocument.SelectSingleNode("rss/channel"); //创建标题 XmlElement imageTitleElement = xmlDocument.CreateElement("title"); imageTitleElement.InnerText = img.title; imgElement.AppendChild(imageTitleElement); //创建地址 XmlElement imageUrlElement = xmlDocument.CreateElement("url"); imageUrlElement.InnerText = img.url; imgElement.AppendChild(imageUrlElement); //创建高度 XmlElement imageHeightElement = xmlDocument.CreateElement("height"); imageHeightElement.InnerText = img.height.ToString(); imgElement.AppendChild(imageHeightElement); //创建长度 XmlElement imageWidthElement = xmlDocument.CreateElement("width"); imageWidthElement.InnerText = img.width.ToString(); imgElement.AppendChild(imageWidthElement); //将图像节点添加到频道节点里面 channelElement.AppendChild(imgElement); return xmlDocument; } /// <summary> /// 添加项信息 /// </summary> /// <param name="xmlDocument"></param> /// <param name="item"></param> /// <returns></returns> private static XmlDocument AddRssItem (XmlDocument xmlDocument, RssItem item) { XmlElement itemElement = xmlDocument.CreateElement("item"); XmlNode channelElement = xmlDocument.SelectSingleNode("rss/channel"); //创建标题 XmlElement itemTitleElement = xmlDocument.CreateElement("title"); XmlCDataSection cDataTitleSection = xmlDocument.CreateCDataSection(item.title); itemTitleElement.AppendChild(cDataTitleSection); itemElement.AppendChild(itemTitleElement); //创建日期 XmlElement pubDateElement = xmlDocument.CreateElement("pubDate"); pubDateElement.InnerText = XmlConvert.ToString(item.pubDate.ToUniversalTime(), "yyyy-MM-ddTHH:mm:ss"); itemElement.AppendChild(pubDateElement); //添加连接 XmlElement itemLinkElement = xmlDocument.CreateElement("link"); itemLinkElement.InnerText = item.link; itemElement.AppendChild(itemLinkElement); //创建描述 XmlElement itemDescriptionElement = xmlDocument.CreateElement("description"); XmlCDataSection cDataDescriptionSection = xmlDocument.CreateCDataSection(item.description); itemDescriptionElement.AppendChild(cDataDescriptionSection); itemElement.AppendChild(itemDescriptionElement); //创建类型 XmlElement itemcatalogElement = xmlDocument.CreateElement("catalog"); itemcatalogElement.InnerText = item.catalog; itemElement.AppendChild(itemcatalogElement); //将RssItem添加到频道节点里面 channelElement.AppendChild(itemElement); return xmlDocument; } } }

    根据特定的需要,可以先将数据读取到列表里面,然后遍历列表,调用上述方法,生成Xml字符串。

    这个字符串就是RS用到XML字符串了。也可以入aspx文件,然后用 <link type="application/rss+xml" rel="alternate" href="rssfeed.aspx">调用下RSS文件,软件就会自动提示有RRS信息了。

0
相关文章