Use C # to generate and parse RSS, and support RSS2.0 and Atom formats

Source: Internet
Author: User

RSS is already very popular, and almost all well-known and not-famous websites provide RSS services.

This document describes what RSS is used in. Net.

1.So what is RSS?

RSS is a standard message source format used to publish websites that frequently update materials, such as blogs and news. An RSS file, also known as a abstract, an online excerpt, an update, or a channel, contains full text or excerpt text, and adds attribute data. RSS allows publishers to automatically publish information, and enables readers to aggregate and regularly update the network excerpt of different websites. RSS can be read through software based on web pages or desktops, such as RSS readers and news aggregators. It also performs regular update checks and automatic downloads. For details, see RSS introduction.

2. RSSWhat is the format?

Currently, RSS supports two standard formats: RSS2.0 and Atom1.0.

3.How to Make RSS and parse RSS?

In. NET4/3.5, MS integrates RSS objects. It makes it easy to parse and create RSS.

First reference System. ServiceModel

Code:

Using System. ServiceModel. Syndication;

 

The method for parsing RSS and Atom is as follows:

// General RSS resolution method protected void ShowRSS (string rssURI) {SyndicationFeed sf = SyndicationFeed. load (XmlReader. create (rssURI); textBox1.Text + = "title:" + sf. title. text + "\ r \ n"; if (sf. links. count> 0) textBox1.Text + = "Link:" + sf. links [0]. uri. toString () + "\ r \ n"; if (sf. authors. count> 0 &&! String. isNullOrEmpty (sf. authors [0]. uri) textBox1.Text + = "Link:" + sf. authors [0]. uri. toString () + "\ r \ n"; textBox1.Text + = "pubDate:" + sf. lastUpdatedTime. toString ("yyyy-MM-dd HH: mm: ss") + "\ r \ n"; foreach (SyndicationItem it in sf. items) {textBox1.Text + = "\ r \ n ----------------------------------------------------- \ r \ n"; textBox1.Text + = "title:" + it. title. text + "\ r \ n"; if (it. links. count> 0) te XtBox1.Text + = "Link:" + it. links [0]. uri. toString () + "\ r \ n"; textBox1.Text + = "PubDate:" + it. publishDate. toString ("yyyy-MM-dd HH: mm: ss") + "\ r \ n"; if (it. summary! = Null) textBox1.Text + = "Summary:" + it. Summary. Text + "\ r \ n"; if (it. Content! = Null) textBox1.Text + = "Content:" + (TextSyndicationContent) it. Content). Text + "\ r \ n"; Application. DoEvents ();}}

  

Explanation: This method can parse the RSS2.0 and Atom formats. The input parameter is an rss xml file path or URL.

 

Example 1 of ShowRSS method call (PARSE Atom ):

// Parse the RSS of the blog site. The RSS version is Atom, which can be downloaded from http://feed.cnblogs.com/blog/u/18638/rss. ShowRSS (Application. StartupPath + "\ cnblogs. xml"); // modify it to the RSS address of the blog garden for testing // ShowRSS ("http://feed.cnblogs.com/blog/u/18638/rss ");

  

Example 2 of ShowRSS method call (parsing RSS2.0 ):

// Parse entrepreneurial RSS, Which is RSS2.0 ShowRSS (Application. startupPath + "\ cyb. xml "); // modify to entrepreneurial RSS address for testing // ShowRSS (" http://www.cyzone.cn/rss ");

  

Explanation: You can test the real website. The preceding two websites are in the RSS2.0 format and Atom format.

 

Method for generating RSS2.0:

// Generate RSS2.0 SyndicationFeed feed = new SyndicationFeed ("blog garden _ DotNet notes", "interest is the best teacher. ", New Uri (" http://cnblogs.com/tuyile006 ")," FeedID, such as: uuid: 0913a2a5-6900-42a0-a3ab-2ba6a1706b03; id = 10373 ", DateTime. now); List <SyndicationItem> items = new List <SyndicationItem> (); SyndicationItem item1 = new SyndicationItem (); item1.Title = new TextSyndicationContent ("blog title, for example: parse and generate RSS or Atom "); item1.Content = SyndicationContent. createPlaintextContent ("Body: This article describes how. net to generate and parse Rss and Atom ...... "); Item1.Summary = SyndicationContent. createPlaintextContent ("Abstract: This article describes how. "); item1.PublishDate = DateTime. now; items. add (item1); SyndicationItem item2 = new SyndicationItem (); item2.Title = new TextSyndicationContent ("blog Title 2, for example :. net notes "); item2.Content = SyndicationContent. createPlaintextContent ("Body: interest is the best teacher ...... "); Item2.Summary = SyndicationContent. createPlaintextContent ("Summary: John's blog. net notes "); item2.PublishDate = DateTime. now; items. add (item2); // cyclically Add ...... Feed. Items = items; XmlWriter xmlWriter = XmlWriter. Create ("YourRSSFile. xml"); feed. SaveAsRss20 (xmlWriter); xmlWriter. Close ();

  

Explanation: generating xml in the RSS2.0 and Atom formats is only different in the final saving method. One is SaveAsRss20, the other is SaveAsAtom10, and the process of creating SyndicationFeed is the same.

 

The program interface is as follows:

 

 

Program download: Demo Code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.