web2.0時(shí)是以blog,wike,tag,rss等技術(shù)為代表的以個(gè)性化為中心的新一代互聯(lián)網(wǎng)模式,rss比起blog等名詞似乎還不算太熱。但打開(kāi)網(wǎng)頁(yè)仍是遍布了rss,xml等醒目的圖標(biāo),打開(kāi)頁(yè)面mathon瀏覽器也是一個(gè)勁的提示有新的rss連接,前一段一個(gè)項(xiàng)
目需要,自己寫(xiě)了一個(gè).net下面生成rss信息的類(lèi),如下:
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頻道結(jié)構(gòu)
18 struct rsschannel
19 {
20 public string title;//標(biāo)題
21 public string link;//連接
22 public string language;//語(yǔ)言
23 public string description;//描述
24 public string webmaster;//發(fā)布者
25 }
26
27 //rss圖片信息
28 struct rssimage
29 {
30 public string url;//地址
31 public string title;//標(biāo)題
32 public int height ;//高度
33 public int width;//長(zhǎng)度
34 }
35
36 //rss項(xiàng)結(jié)構(gòu)
37 struct rssitem
38 {
39 public string title;//標(biāo)題
40 public string catalog;//類(lèi)別
41 public string link;//連接
42 public datetime pubdate;//發(fā)布日期
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 //聲明創(chuàng)建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 //添加標(biāo)題
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 //添加語(yǔ)言
117 xmlelement languageelement = xmldocument.createelement("language");
118 languageelement.innertext = channel.language;
119 channelelement.appendchild(languageelement);
120
121 //添加發(fā)布者
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 //創(chuàng)建標(biāo)題
137 xmlelement imagetitleelement = xmldocument.createelement("title");
138 imagetitleelement.innertext = img.title;
139 imgelement.appendchild(imagetitleelement);
140
141 //創(chuàng)建地址
142 xmlelement imageurlelement = xmldocument.createelement("url");
143 imageurlelement.innertext = img.url;
144 imgelement.appendchild(imageurlelement);
145
146 //創(chuàng)建高度
147 xmlelement imageheightelement = xmldocument.createelement("height");
148 imageheightelement.innertext = img.height.tostring();
149 imgelement.appendchild(imageheightelement);
150
151 //創(chuàng)建長(zhǎng)度
152 xmlelement imagewidthelement = xmldocument.createelement("width");
153 imagewidthelement.innertext = img.width.tostring();
154 imgelement.appendchild(imagewidthelement);
155
156 //將圖像節(jié)點(diǎn)添加到頻道節(jié)點(diǎn)里面
157 channelelement.appendchild(imgelement);
158 return xmldocument;
159
160 }
161
162
163 /// <summary>
164 /// 添加項(xiàng)信息
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 //創(chuàng)建標(biāo)題
175 xmlelement itemtitleelement = xmldocument.createelement("title");
176 xmlcdatasection cdatatitlesection = xmldocument.createcdatasection(item.title);
177 itemtitleelement.appendchild(cdatatitlesection);
178 itemelement.appendchild(itemtitleelement);
179
180 //創(chuàng)建日期
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 //創(chuàng)建描述
191 xmlelement itemdescriptionelement = xmldocument.createelement("description");
192 xmlcdatasection cdatadescriptionsection = xmldocument.createcdatasection(item.description);
193 itemdescriptionelement.appendchild(cdatadescriptionsection);
194 itemelement.appendchild(itemdescriptionelement);
195
196
197 //創(chuàng)建類(lèi)型
198 xmlelement itemcatalogelement = xmldocument.createelement("catalog");
199 itemcatalogelement.innertext = item.catalog;
200 itemelement.appendchild(itemcatalogelement);
201
202 //將rssitem添加到頻道節(jié)點(diǎn)里面
203 channelelement.appendchild(itemelement);
204
205 return xmldocument;
206 }
207 }
208}
根據(jù)特定的需要,可以先將數(shù)據(jù)讀取到列表里面,然后遍歷列表,調(diào)用上述方法,生成xml字符串。
這個(gè)字符串就是rs用到xml字符串了。也可以入aspx文件,然后用 <link type="application/rss+xml" rel="alternate" href="rssfeed.aspx">調(diào)用下rss文件,馬桶等軟件就會(huì)自動(dòng)提示有rrs信息了
新聞熱點(diǎn)
疑難解答
圖片精選