Thứ Sáu, 21 tháng 1, 2011

Hướng dẫn cách tạo RSS trong ASP.NET

Chắc Các bạn đã từng nhìn thấy hoặc từng sử dụng chức năng RSS của một website. Bài viết này sẽ hướng dẫn các bạn làm thế nào để tạo một RSS cho một website, Thật Đơn giản.

Đây là struct cho một RSS Channel

1
2
3
4
5
6
public struct RssChannel
{
public string Title;
public string Link;
public string Description;
}

Còn đây là struct cho một RSS Item

1
2
3
4
5
6
public struct RssItem
{
public string Title;
public string Link;
public string Description;
}

Sau đó chúng ta viết 2 methods để tạo channel và tạo các item trong channel như sau:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
private static XmlDocument addRssChannel(XmlDocument xmlDocument, RssChannel channel)
{
XmlElement channelElement = xmlDocument.CreateElement("channel");
XmlNode rssElement = xmlDocument.SelectSingleNode("rss");
rssElement.AppendChild(channelElement);
XmlElement titleElement = xmlDocument.CreateElement("title");
titleElement.InnerText = channel.Title;
channelElement.AppendChild(titleElement);
XmlElement linkElement = xmlDocument.CreateElement("link");
linkElement.InnerText = channel.Link;
channelElement.AppendChild(linkElement);
XmlElement descriptionElement = xmlDocument.CreateElement("description");
descriptionElement.InnerText = channel.Description;
channelElement.AppendChild(descriptionElement);
// Generator information
XmlElement generatorElement = xmlDocument.CreateElement("generator");
generatorElement.InnerText = "Your RSS Generator name and version ";
channelElement.AppendChild(generatorElement);
return xmlDocument;
}

và Đây là code để tạo một item cho channel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static XmlDocument addRssItem(XmlDocument xmlDocument, RssItem item)
{
XmlElement itemElement = xmlDocument.CreateElement("item");
XmlNode channelElement = xmlDocument.SelectSingleNode("rss/channel");
XmlElement titleElement = xmlDocument.CreateElement("title");
titleElement.InnerText = item.Title;
itemElement.AppendChild(titleElement);
XmlElement linkElement = xmlDocument.CreateElement("link");
linkElement.InnerText = item.Link;
itemElement.AppendChild(linkElement);
XmlElement descriptionElement = xmlDocument.CreateElement("description");
descriptionElement.InnerText = item.Description;
itemElement.AppendChild(descriptionElement);
// append the item
channelElement.AppendChild(itemElement);
return xmlDocument;
}

Sau đó ta lập một class để sinh ra RSS, ví dụ tên class là NewsRss, class này sẽ chứa 2 cái struct trên và cả 2 methods trên.

Code của class thì chỉ quan trọng cái constructor để tạo XmlDocument và một số methods nhằm tạo channel, add item, trả về

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
xml document:
public NewsRSS()
{
_rss = new XmlDocument();
XmlDeclaration xmlDeclaration = _rss.CreateXmlDeclaration("1.0", "utf-8", null);
_rss.InsertBefore(xmlDeclaration, _rss.DocumentElement);
XmlElement rssElement = _rss.CreateElement("rss");
XmlAttribute rssVersionAttribute = _rss.CreateAttribute("version");
rssVersionAttribute.InnerText = "2.0";
rssElement.Attributes.Append(rssVersionAttribute);
_rss.AppendChild(rssElement);
}
public void AddRssChannel(RssChannel channel)
{
_rss = addRssChannel(_rss, channel);
}
public void AddRssItem(RssItem item)
{
_rss = addRssItem(_rss, item);
}
public string RssDocument
{
get
{
return _rss.OuterXml;
}
}
public XmlDocument RssXMLDocument
{
get
{
return _rss;
}
}

Về bây giờ ta tạo các instant của class này mà sử dụng thôi:

Ví dụ tạo rss document và tạo channel:

1
2
3
4
5
6
7
8
9
10
11
NewsRSS rss = new NewsRSS();
NewsRSS.RssChannel channel = new NewsRSS.RssChannel();
channel.Title = "Zensoft Website";
channel.Link = "http://zensoft.vn";
channel.Description = "Website Chia xẻ Thông tin về CNTT.";
rss.AddRssChannel(channel);

Ví dụ tạo rss item:

1
2
3
4
5
6
7
8
9
NewsRSS.RssItem item = new NewsRSS.RssItem();
item.Link = "http://zensoft.vn/ShowCategory.aspx?ID=9";
item.Description = "Các Bài Viết Hướng Dẫn về lập Trình";
rss.AddRssItem(item);

Cuối cùng, write cái Rss trên thành Xml document ra Response object trại cái ASPX page tương tác với bên ngoài:

1
2
3
4
5
6
7
Response.Clear();
Response.ContentType = "text/xml";
Response.Write(rss.RssDocument);
Response.End();

Theo Zensoft

Nguồn : Hướng dẫn cách tạo RSS trong ASP.NET

0 nhận xét:

Đăng nhận xét