WEB 2.0-era RSS. NET implementation

Source: Internet
Author: User
Rss|web

Web2.0 is a blog,wike,tag,rss and other technologies as the representative of the new generation of personalized-centric Internet model, RSS than blog and other nouns seem not too hot. But open the page is still all over the rss,xml and other eye-catching icons, open the page Mathon browser is also a hint of a new RSS connection, a previous item

The eye needs to write one himself. NET generates the following classes of RSS information, as follows:


1using System;
2using System.Xml;
3using System.Collections;
4using System.Globalization;
5using system.web;
6
7namespace BLRL
8{
9///<summary>
Ten///Summary description for Rss.
One///</summary>
public class Rss
13 {
The const string Dublincorenamespaceuri = @ "HTTP://PURL.ORG/DC/ELEMENTS/1.1/";
Const string Slashnamespaceuri = @ "http://purl.org/rss/1.0/modules/slash/";
Const string Syndicationnamespaceuri = @ "http://purl.org/rss/1.0/modules/syndication/";
//RSS Channel Structure
struct RSSChannel
19 {
public string title;//Title
public string link;//Connection
public string language;//language
public string description;//Description
public string webmaster;//Publisher
25}
26
//rss Picture Information
struct Rssimage
29 {
public string url;//Address
To public string title;//title
the public int height;/height
width;//int Length
34}
35
The structure of//rss
Panax Notoginseng struct RssItem
38 {
title;//String Header
public string catalog;//Category
A-public string link;//connection
The public DateTime pubdate;//release date
The public string description;//description
44
45}
Public Rss ()
47 {
48//
Todo:add//constructor logic here
50//
51}
///<summary>
53///Add RSS version Information
///</summary>
///<param name= "XmlDocument" ></param>
///<returns></returns>
public static XmlDocument Addrsspreamble (XmlDocument XmlDocument)
58 {
59//Declaration to create 1.0 version of XML
XmlDeclaration XmlDeclaration = xmldocument.createxmldeclaration ("1.0", "Utf-8", null);
Xmldocument.insertbefore (XmlDeclaration, xmldocument.documentelement);
62
XmlElement rsselement = xmldocument.createelement ("RSS");
64
XmlAttribute Rssversionattribute = Xmldocument.createattribute ("version");
Rssversionattribute.innertext = "2.0";
RssElement.Attributes.Append (Rssversionattribute);
Xmldocument.appendchild (rsselement);
69
70
XmlAttribute Dubliccorenamespaceuriattribute = Xmldocument.createattribute ("XMLNS:DC");
Dubliccorenamespaceuriattribute.innertext = Dublincorenamespaceuri;
RssElement.Attributes.Append (Dubliccorenamespaceuriattribute);
74
XmlAttribute Slashnamespaceuriattribute = Xmldocument.createattribute ("Xmlns:slash");
The Slashnamespaceuriattribute.innertext = Slashnamespaceuri;
RssElement.Attributes.Append (Slashnamespaceuriattribute);
78
XmlAttribute Syndicationnamespaceuriattribute = Xmldocument.createattribute ("Xmlns:sy");
Syndicationnamespaceuriattribute.innertext = Syndicationnamespaceuri;
Bayi RssElement.Attributes.Append (Syndicationnamespaceuriattribute);
82
83
The return XmlDocument;
85}
86
///<summary>
88///Add Channel
///</summary>
///<param name= "XmlDocument" ></param>
///<param name= "channel" ></param>
///<returns></returns>
Addrsschannel private static XmlDocument (XmlDocument XmlDocument, RSSChannel Channel)
94 {
XmlElement channelelement = xmldocument.createelement ("channel");
XmlNode rsselement = Xmldocument.selectsinglenode ("RSS");
97
Rsselement.appendchild (channelelement);
99
100//Add title
XmlElement channeltitleelement = xmldocument.createelement ("title");
102 Channeltitleelement.innertext = Channel.title;
Channelelement.appendchild (channeltitleelement);
104
105//Add Connection
The XmlElement channellinkelement = xmldocument.createelement ("link");
Channellinkelement.innertext = Channel.link;
108 Channelelement.appendchild (channellinkelement);
109
110//Add description
XmlElement channeldescriptionelement = xmldocument.createelement ("description");
112 Xmlcdatasection cdatadescriptionsection = Xmldocument.createcdatasection (channel.description);
113 Channeldescriptionelement.appendchild (cdatadescriptionsection);
114 Channelelement.appendchild (channeldescriptionelement);
115
116//Add language
117 XmlElement languageelement = xmldocument.createelement ("language");
118 Languageelement.innertext = Channel.language;
119 Channelelement.appendchild (languageelement);
120
121//Add Publisher
122 XmlElement webmasterelement = xmldocument.createelement ("webmaster");
123 Webmasterelement.innertext = Channel.webmaster;
124 Channelelement.appendchild (webmasterelement);
125
126 return XmlDocument;
127}
128
129
130//Add Rssimage
131 private static XmlDocument Addrssimage (XmlDocument XmlDocument, Rssimage img)
132 {
The XmlElement imgelement = xmldocument.createelement ("image");
134 XmlNode channelelement = Xmldocument.selectsinglenode ("Rss/channel");
135
136//Create title
137 XmlElement imagetitleelement = xmldocument.createelement ("title");
138 Imagetitleelement.innertext = Img.title;
139 Imgelement.appendchild (imagetitleelement);
140
141//Create address
XmlElement imageurlelement = xmldocument.createelement ("url");
143 Imageurlelement.innertext = Img.url;
144 Imgelement.appendchild (imageurlelement);
145
146//Create height
147 XmlElement imageheightelement = xmldocument.createelement ("height");
148 Imageheightelement.innertext = img.height.ToString ();
149 Imgelement.appendchild (imageheightelement);
150
151//Create length
152 XmlElement imagewidthelement = xmldocument.createelement ("width");
153 Imagewidthelement.innertext = img.width.ToString ();
154 Imgelement.appendchild (imagewidthelement);
155
156///Add the image node to the channel node
157 Channelelement.appendchild (imgelement);
158 return XmlDocument;
159
160}
161
162
163///<summary>
164///Add Item information
165///</summary>
166///<param Name= "XmlDocument" ></param>
167///<param name= "Item" ></param>
///<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//Create title
175 XmlElement itemtitleelement = xmldocument.createelement ("title");
176 xmlcdatasection cdatatitlesection = Xmldocument.createcdatasection (Item.title);
177 Itemtitleelement.appendchild (cdatatitlesection);
178 Itemelement.appendchild (itemtitleelement);
179
180//Date Created
181 XmlElement pubdateelement = xmldocument.createelement ("pubdate");
Pubdateelement.innertext = xmlconvert.tostring (Item.pubDate.ToUniversalTime (), "Yyyy-mm-ddthh:mm:ss");
183 Itemelement.appendchild (pubdateelement);
184
185//Add Connection
186 XmlElement itemlinkelement = xmldocument.createelement ("link");
187 Itemlinkelement.innertext = Item.link;
188 Itemelement.appendchild (itemlinkelement);
189
190//Create description
191 XmlElement itemdescriptionelement = xmldocument.createelement ("description");
Xmlcdatasection cdatadescriptionsection = xmldocument.createcdatasection (item.description);
193 Itemdescriptionelement.appendchild (cdatadescriptionsection);
194 itemelement.appendchild (itemdescriptionelement);
195
196
197//Create type
198 XmlElement itemcatalogelement = xmldocument.createelement ("catalog");
199 Itemcatalogelement.innertext = Item.catalog;
Itemelement.appendchild (itemcatalogelement);
201
202//Add RssItem to the channel node
203 Channelelement.appendchild (itemelement);
204
205 return XmlDocument;
206}
207}
208}

Depending on your specific needs, you can read the data to the list, then iterate through the list, call the above method, and generate an XML string.

This string is the XML string used by Rs. You can also enter the ASPX file, and then use the <link type= "Application/rss+xml" rel= "alternate" href= "rssfeed.aspx" > Call the RSS file, The toilet software will automatically prompt for RRs information.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.