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信息了
新闻热点
疑难解答
图片精选