Output RSS Feeds in an ASP. NET MVC Web application

Source: Internet
Author: User
Tags visual studio 2010

RSS Full name Really Simple syndication. Some of the more frequently updated sites can be RSS-ready for subscribers to get updated information quickly. RSS documents are subject to the XML specification, which must include headings, links, descriptive information, and information such as publication time, last update time, and so on.

This article describes generating an XML document from LINQ to XML and outputting it in an ASP. NET MVC Web application.

Before you generate an RSS document, take a quick look at the structure of RSS. The root node RSS has a channel node, and some sub-nodes (title,link,description) of the channel node contain some descriptive information about the RSS. The channel can contain multiple item nodes to represent multiple content information, such as articles in a blog, posts in a forum.

Code1 <rss version= "2.0" >
2 <channel>
3 <title>channel title </title>
4 <link> Web address </link>
5 <description>channel Description </description>
6 <item>
7 <title> content 1 title </title>
8 <description> Content 1 Description </description>
9 <link> content 1 Links </link>
Ten </item>
<item>
<title> Content 2 title </title>
<description> Content 2 Description </description>
<link> Content 2 Links </link> </item>
</channel>
</rss>

1. Generate a document similar to the above with LINQ to XML.

1.1 Create a new XDocument, add the root node and the related property description.

Code1 XDocument doc = new XDocument (
2 new Xdeclaration ("1.0", "Utf-8", "yes"),//XML document Declaration
3 new XElement ("RSS",//root node
4 new XAttribute ("version", "2.0"),//RSS node Properties
5 new XElement (Channel//RSS sub-node channel
6))); )));

1.2 handles the channel node and its associated description.

Code1 XElement channel = new XElement ("channel"); Channel node
2 channel. ADD (New xelement[]{
3 new XElement ("title", "Test"),//channel title
4 new XElement ("link", "http://localhost"),//page link
5 new XElement ("description", "Test RSS")//Channel description
6});

1.3 Add the content information to the channel node, rssfeedlist is the list<rssfeed> type. Because the item number is not fixed, a foreach is used to add every content information in the list to the channel.

Code1 foreach (Var rssfeed in rssfeedlist)//handling for each element in the RssFeed collection
2 {
3 XElement item = new XElement ("Item", new xelement[]{//Generate a new item node
4 new XElement ("title", Rssfeed.title),//Add child nodes for the new item node
5 new XElement ("description", rssfeed.description),
6 new XElement ("link", Rssfeed.link),
7 New XElement ("PubDate", Rssfeed.publishdate)
8});
9 channel.    ADD (item); Add a new item node to the channel
10}

2. Create a Rssfeedresult class

We write a Rssfeedresult class that inherits from ActionResult to return RSS in the controller of ASP. For this section, refer to the previous article, "Let the ASP. NET MVC page return different types of content."

Code1 public class Rssfeedresult:actionresult
2 {
3 list<rssfeed> Data {get; set;}
4
5 Public Rssfeedresult (list<rssfeed> data)
6 {
7 data = data;
8}
9
public override void Executeresult (ControllerContext context)
11 {
if (context = = null)
13 {
New ArgumentNullException ("context");
15}
16
Httpresponsebase response = context. Httpcontext.response;
Response.    ContentType = "Text/xml"; Set the contenttype in the HTTP header
XDocument result= rssfeedhelper.getrssfeed (Data); Get XML data
Response. Write (result.    ToString ()); Writing XML data to response
21}
22}

3. Using in Controller

We simply call the Rssfeedresult (Rssfeedlist) method in the controller to return to the RSS page.

Public Rssfeedresult Rss ()
{
Add 2 test data
RssFeed r1 = new RssFeed {Description = "Test1", Link = "HTTP://LOCALHOST/1", Title = "Test1", publishdate = DateTime.Now };
RssFeed r2 = new RssFeed {Description = "Test2", Link = "HTTP://LOCALHOST/2", Title = "Test2", publishdate = DateTime.Now };
list<rssfeed> rssfeedlist = new list<rssfeed> ();
Rssfeedlist.add (R1);
Rssfeedlist.add (R2);

Back to RSS
return new Rssfeedresult (rssfeedlist);
}

Sample Download (Visual Studio 2010)

Output RSS Feeds in an ASP. NET MVC Web application

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.