Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web
Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web Diễn đàn thiết kế web, thiết kế web, thiết kế website, thiet ke web, thiet ke website, tìm hiểu web

Quay lại   Dien dan thiet ke web > Lập trình (code) > Thiết kế lập trình ASP/. NET

Thiết kế lập trình ASP/. NET Các vấn đề liên quan .NET


Chủ đề tương tự
Chủ đề Người mở Chủ đề Diễn đàn Trả lời Bài mới gửi
Logo - Những vấn đề cần quan tâm. hoangduc Kiến thức và kỹ thuật thiết kế liên quan đồ họa 1 02-01-2010 05:29 PM
Triển khai IPsec Server và Domain Isolation bằng Windows Server 2008 Group Policy- P3 buonngu Cấu hình Domain - Hosting Server 0 01-09-2010 04:46 PM
ASP.NET Web Service hay .NET Remoting Thienthan Thiết kế lập trình ASP/. NET 0 10-30-2009 05:47 PM
ASP.NET Web Service hay .NET Remoting buonngu Thiết kế lập trình ASP/. NET 0 07-24-2009 09:07 PM
.NET Framework - Tâm điểm của Nền tảng .NET P2 ziulong Web Framework 0 10-31-2008 04:54 PM

Gửi trả lời
 
LinkBack Công Cụ Dạng sắp xếp bài
  #1 (permalink)  
Cũ 03-09-2010, 09:09 PM
Thienthan Thienthan đang Offline
Senior Member
 
Ngày gia nhập: Oct 2009
Bài gửi: 407
Mặc định 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.

Trước hết, chúng ta tìm hiểu định nghĩa về RSS là gì.
RSS (Really Simple Syndication) là định dạng dữ liệu dựa theo chuẩn XML được sử dụng để chia sẻ và phát tán nội dung Web. Việc sử dụng các chương trình đọc tin (News Reader, RSS Reader hay RSS Feeds) sẽ giúp bạn luôn xem được nhanh chóng tin tức mới nhất
Mỗi tin dưới dạng RSS sẽ gồm : Tiêu đề, tóm tắt nội dung và đường dẫn nối đến trang Webchứa nội dung đầy đủ của tin.
Quay trở lại về chương trình. Trước hết, chúng ta sẽ tạo 2 struct để thể hiện kênh (channel) và nội dung. Về sau chỉ việc dùng 2 channel này để tạo các kênh và nội dung thông tin
Đây là struct cho một RSS Channel
public struct RssChannel
{
public string Title;
public string Link;
public string Description;
}

Còn đây là struct cho một RSS Item
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:
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:
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ề
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:
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:

NewsRSS.RssItem item = new NewsRSS.RssItem();

item.Title = "Lập Trình";

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:
Response.Clear();

Response.ContentType = "text/xml";

Response.Write(rss.RssDocument);

Response.End();

Các Bạn có thể download mã nguồn tại đây.
 Nuoc hoaMy phamTrang diemLam depQua tang
Trả lời kèm theo trích dẫn

10 Chủ đề mới nhất của Thienthan
Chủ đề Chuyên mục Người gởi sau cùng Trả lời Lần đọc Bài mới gửi
Chọn từ khóa nào để đứng đầu các kết quả tìm kiếm? Kinh doanh, quảng bá trực tuyến Thienthan 0 83 04-07-2010 05:49 PM
Flash CS4 bài 16: Áp dụng color effect để tạo... Flash Thienthan 0 110 04-07-2010 05:45 PM
Truy cập Internet với phí siêu rẻ Các vấn đề khác liên quan đến Mạng Thienthan 0 115 04-07-2010 05:39 PM
Các bài hướng dẫn cơ bản về Flash P5 Flash Thienthan 0 79 04-07-2010 05:38 PM
Các bài hướng dẫn cơ bản về Flash P4 Flash namseo53 1 447 04-07-2010 05:37 PM
Các bài hướng dẫn cơ bản về Flash P3 Flash Thienthan 0 80 04-07-2010 05:36 PM
Các bài hướng dẫn cơ bản về Flash P2 Flash Thienthan 0 94 04-07-2010 05:35 PM
Các bài hướng dẫn cơ bản về Flash P1 Flash Thienthan 0 107 04-07-2010 05:34 PM
Phòng chống virus lây qua USB Anti virus - Spyware - Hacking Thienthan 0 506 04-07-2010 05:32 PM
How to Do Everything with Photoshop 7 E-Book Thienthan 0 131 03-11-2010 04:35 PM

Gửi trả lời

Công Cụ
Dạng sắp xếp bài

Quyền hạn Gửi bài
Bạn không thể gửi chủ đề
Bạn không thể gửi trả lời
Bạn không thể gửi đính kèm
Bạn không thể sửa bài của mình

Mã BB đang Mở
[IMG] đang Mở
Mã HTML đang Tắt
Trackbacks are Mở
Pingbacks are Mở
Refbacks are Mở


Viec Lam - Việc làm - Tuyen Dung - Tuyển Dụng - Tìm Việc Làm và Đăng Tuyển Dụng Miễn Phí Thiet ke web, Thiết kế web, Thiết kế website đẹp chuyên nghiệp Tổ chức sự kiện, Công ty Truyền thông Thương hiệu Quốc tế, e marketing Hoa tươi, hoa đẹp, Shop hoa, điện hoa, hoa cưới
To chuc su kien VIP - Tổ chức sự kiện - Công ty tổ chức sự kiện Balo , Ba lô, Balo Laptop, Balo tui xach, balo cap hoc sinh in ky thuat so, ke x, kệ x, standy, standee, rollup banner, banner stand dong phuc the thao, do bong da, quan ao bong ro, dong phuc lop

Tất cả thời gian được tính theo GMT +7. Bây giờ là 12:55 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.