📜 ⬆️ ⬇️

Generating RSS feeds using the Syndication class

I do not understand why such a wonderful framework as .NET has such a small popularity in the CIS countries. After all, he has everything you need to create projects of any level of complexity, and especially for web projects. And it is no worse, for example, nowadays fashionable Ruby on Rails, it’s even better. By the way, if you like the concept of MVC programming so much, you can try the ASP.NET MVC framework.

Because of this, my article assumes two target groups: people who are already writing on .NET and who will be interested to learn about generating RSS feeds, and not .NET programmers, whom I will try to show the ease and elegance of solving various tasks in this framework using the example generating rss. So, let's begin. We will use .NET 3.5 and C #.

As you know, feeds can be in two formats: RSS and ATOM. So, it is absolutely not important to us, since we first create the feed itself, and only then decide in which format to save it.
The first step will naturally be to create a new site in Visual Studio. After that we add “System.ServiceModel.Web” to References, if it isn’t there yet, open Default.aspx.cs and add the following lines to the very beginning:
using System.ServiceModel.Syndication;
using System.Xml;
using System.Collections.Generic;

For those who do not know, these are just links to the namespace, which contains the classes we need.
All subsequent code should be added to Page_Load in order.
Now create the feed object itself using the SyndicationFeed class:
SyndicationFeed feed = new SyndicationFeed ();
// The name that will be visible in any reader
feed.Title = new TextSyndicationContent
( "My test RSS feed." );
// Copyright Block
feed.Copyright = new TextSyndicationContent
( "2008" );
// Description and name of the feed generator.
feed.Description = new TextSyndicationContent
( "Automatically generated feed" );
feed.Generator = "Maxter's RSS Feed Generator" ;
// Well, the link to the source
SyndicationLink link = new SyndicationLink ();
link.Title = "Habr.ru" ;
link. Uri = new Uri ( " habrahabr.ru " );
feed.Links.Add (link);

It seems everything is simple and clear. The only thing I would like to draw attention to: to add any text information, except the Generator property, to the feed, the TextSyndicationContent class is used. Well, here you will be prompted by IntelliSense, do not get confused.
After that we add the actual posts to the feed (for example, one is enough for us):
SyndicationItem item = new SyndicationItem ();
item.Id = Guid .NewGuid (). ToString ();
item.Title = new TextSyndicationContent
( "Hello, HabraHabr!" );
item.Summary = new TextSyndicationContent
( "There is a short description here" );
item.Content = new TextSyndicationContent
( "And here is the full text" );

It seems everything is clear too: we create a record, automatically assign an ID, a name, a short description and a body. In real life, all this information will be obtained from the database or from somewhere else, but we have just an example, so we will be static. You can also add links to the record and the author, but we will not do this, because it is rather simple if you want to do it yourself.
Add our entry to the feed:
List <SyndicationItem> items =
new List <SyndicationItem> ();
items.Add (item);
feed.Items = items;

Well, now select the feed format and analyze it. Let it be RSS 2.0:

Response.Clear ();
Response.ContentEncoding =
System.Text. Encoding .UTF8;
Response.ContentType = "text / xml" ;
')
XmlWriter rssWriter = XmlWriter.Create
(Response.Output);
Rss20FeedFormatter rssFormatter =
new Rss20FeedFormatter (feed);
rssFormatter.WriteTo (rssWriter);
rssWriter.Close ();

Response.End ();

Here we first clear the output buffer, select the encoding and MIME type. Then create an XmlWriter that will write formatted XML to the output buffer. Well, in turn, XmlWriter, we, in turn, submit our feed in the format of RSS 2.0, which we classify for us with the Rss20FeedFormatter class (you can also use Atom10FeedFormatter for Atom 1.0, respectively).

Everything! You can compile the site and, if everything is done correctly, in the browser you will see our feed, which you can already subscribe to.
Thanks for attention. The code was highlighted and formatted using Source Code Highlighter , MSDN and some foreign blogs helped me in writing the article.
By the way, in IE 7 our page will look like this:
Picture_5.png - image uploaded to Picamatic
upd: In the course of the discussion, I found out a slightly more beautiful version, see this comment , thanks for saying RAMe0

Source: https://habr.com/ru/post/27356/


All Articles