Web 2.0 era RSS .NET implementation

xiaoxiao2021-04-04  242

WEB 2.0 is a new generation of Internet models represented by BLOG, WIKE, TAG, RSS and other technologies, and RSS seems to be too hot than the names such as blog. However, open the web is still a hidden icon, which is overwhelmed by RSS, XML. Open the page Mathon browser is also a strong prompt has a new RSS connection, one item in the previous paragraph

Types need to write a class that generates RSS information below, as follows:

1using system.xml; 3using system.collections; 4using system.globalization; 5using system.Web; 6 7NameSpace BLRL 8 {9 ///

10 /// summary description for RSS. 11 /// 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 channel structure 18 struct RssChannel 19 {20 public string title; // Title 21 Public String Link; // Connecting 22 Public String Language; // Language 23 Public String Description; // Description 24 Public String WebMaster; // Publisher 25} 26 27 // RSS Picture Information 28 Struct Rssimage 29 {30 Public String URL; // address 31 public string title; // Title 32 public int Height; // Height 33 p Ublic int width; // length 34} 35 36 // RSS Item 37 Struct Rssitem 38 {39 public string title; // Title 40 public string catalog; // Category 41 public string link; // connection 42 public datetime pubdate; // Release date 43 public string description; // Description 44 45} 46 Public RSS () 47 {48 // 49 // Todo: Add Constructor Logic Here 50 // 51} 52 /// 53 /// Add RSS version information 54 /// 55 /// 56 ///

57 public static XmlDocument AddRssPreamble (XmlDocument xmlDocument) 58 {59 // declaration creates version 1.0 available 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.CreateAt tribute ( "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 returnction xmldocument; 85} 86 87 ///

88 /// Add channel 89 /// 90 /// <

/ Param> 91 /// 92 /// 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); 99100 // add header 101 XmlElement channelTitleElement = xmlDocument.CreateElement ( "title"); 102 channelTitleElement. InnerText = channel.title; 103 channelElement.AppendChild (channelTitleElement); 104105 // Add connection 106 XmlElement channelLinkElement = xmlDocument.CreateElement ( "link"); 107 channelLinkElement.InnerText = channel.link; 108 channelElement.AppendChild (channelLinkElement); 109110 // Add Description 111 Xmlelement ChannelDescriptionElement = XMLDocument.createElement ("Description"); 112 xmlc DataSection cDataDescriptionSection = xmlDocument.CreateCDataSection (channel.description); 113 channelDescriptionElement.AppendChild (cDataDescriptionSection); 114 channelElement.AppendChild (channelDescriptionElement); 115 116 // Add languages ​​117 XmlElement languageElement = xmlDocument.CreateElement ( "language"); 118 languageElement. InnerText = CHANNEL.LANGUAGE; 119 CHANNELEMENT.APpendChild (LanguageElement); 120121 // Add Publisher 122 XmLelement WebMasterelement = XMLDocument.createElement ("WebMaster");

123 webMasterElement.InnerText = channel.webMaster; 124 channelElement.AppendChild (webMasterElement); 125126 return xmlDocument; 127} 128129130 // Add RssImage131 private static XmlDocument AddRssImage (XmlDocument xmlDocument, RssImage img) 132 {133 XmlElement imgElement = xmlDocument.CreateElement ( " image "); 134 XmlNode channelElement = xmlDocument.SelectSingleNode (" rss / channel "); 135136 // Create a header 137 XmlElement imageTitleElement = xmlDocument.CreateElement (" title "); 138 imageTitleElement.InnerText = img.title; 139 imgElement.AppendChild (imageTitleElement); 140141 // Create address 142 XmlElement imageUrlElement = xmlDocument.CreateElement ( "url"); 143 imageUrlElement.InnerText = img.url; 144 imgElement.AppendChild (imageUrlElement); 145146 // Create a height 147 XmlElement imageHeightElement = xmlDocument. CreateElement ("Height"); 148 imageHeightElement.InnerText = img.height.ToString (); 149 imgElement.AppendChild (imageHeightElement); 150151 // Create length 152 XmlElement imageWidthElement = xmlDocument.CreateElement ( "width"); 153 imageWidthElement.InnerText = img.width.ToString () ; 154 imgElement.AppendChild (imageWidthElement); 155156 // add images to a node inside the channel node 157 channelElement.AppendChild (imgElement); 158 return xmlDocument; 159 160} 161162163 ///

164 /// Add item information 165 /// 166 ///

167 /// 168 /// 169 private static XmlDocument AddRssItem (XmlDocument xmlDocument, RssItem item) 170 {171 XmlElement itemElement = xmlDocument.CreateElement ( " item "); 172 XmlNode channelElement = xmlDocument.SelectSingleNode (" rss / channel "); 173174 // create a header 175 XmlElement itemTitleElement = xmlDocument.CreateElement (" title "); 176 XmlCDataSection cDataTitleSection = xmlDocument.CreateCDataSection (item.title); 177 itemTitleElement.AppendChild (cDataTitleSection); 178 itemElement.AppendChild (itemTitleElement); 179180 // creation date 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 // Add connection 186 XmlElement itemLinkElement = xmlDocument.CreateElement ( "link"); 187 itemLinkElement.InnerText = item.link; 188 itemElement.AppendChild (itemLinkElement); 189190 // create a description 191 XmlElement itemDescriptionElement = xmlDocument.CreateElement ( "description"); 192 XmlCDataSection cDataDescriptionSection = XmlDocument.createcdatasection (item.description); 193 itemdescriptionElement.Appendchild (cdatadescriptions); 194 itemlement.appendchild (itemdescriptionelement);

// Create Type 195 196 197 198 XmlElement itemcatalogElement = xmlDocument.CreateElement ( "catalog"); 199 itemcatalogElement.InnerText = item.catalog; 200 itemElement.AppendChild (itemcatalogElement); 201202 // RssItem added to the node inside the channel 203 channelElement.AppendChild ( ItemElement; 204205 Return XMLDocument; 206} 207} 208} Depending on the specific needs, the data can be read in the list, and then traverse the list, call the above method, generate an XML string.

This string is RS to use the XML string. You can also enter the ASPX file, then use

<

LINK

Type

= "Application / RSS XML"

Rel

= "alternate"

HREF

= "rssfeed.aspx"

>

Call the RSS file, toilet and other software will automatically prompt to have RRS information.

Title = "Cool Network Community" href = "rssfeed.aspx" type = "Application / RSS XML" Rel = "alternate" />

http://jillzhang.cnblogs.com/archive/2006/06/11/423086.html title = "Cool Network Community" href = "rssfeed.aspx" type = "Application / RSS XML" Rel = "alternate" / > title = "Cool Network Community" href = "rssfeed.aspx" type = "Application / RSS XML" Rel = "Alternate" /> Title = "Cool Network Community" HREF = "rssfeed.aspx" type = "Application / RSS XML "rel =" alternate "/> Title =" Cool Network Community "href =" rssfeed.aspx "type =" Application / RSS XML "Rel =" Alternate "/> Title =" Cool Network Community "href = "rssfeed.aspx" type = "Application / RSS XML" rel = "alternate" /> title = "cool network community" href = "rssfeed.aspx" type = "Application / RSS XML" Rel = "alternate" / >

转载请注明原文地址:https://www.9cbs.com/read-131721.html

New Post(0)