HowTo: Create RSS Feeds with LINQ to XML (XLinq)

It´s really easy to create XML with LINQ to XML – you can find a HowTo on my german blog.
Now we´ll try to create an RSS Feed with XLINQ

My Projectfiles:

image

The "Rss.ashx" will create your RSS. At first I want to make sure, that my site-visitors detect my nice RSS Feed automatically:

<head runat="server">
    <title>Untitled Page</title>
    <link rel="alternate" href="Rss.ashx" type="application/rss+xml" title="" id="rss" />
</head>

This RSS feature is called "RSS Autodiscovery".

The "Rss.ashx":

We create the head (name, declaration and so on) of the RSS XML in the ProcessRequest Method:

        public void ProcessRequest(HttpContext context)
        {

            XDocument document = new XDocument(
                                    new XDeclaration("1.0", "utf-8", "yes"),
                                    new XElement("rss",
                                        new XAttribute("version", "2.0"),
                                        new XElement("channel", this.CreateElements())
                                       ));

            context.Response.ContentType = "text/xml";
            document.Save(context.Response.Output);
            context.Response.End();
        }

At the end of the method the  XDocument is saved into the Response.Output. Your RSS items are created in the "CreateElements" Method.

The "CreateElements"-Method:

This method returns IEnumberable<XElement> and the elements will be appended the channel-Element (which is created in the ProcessRequest-Method):

private IEnumerable<XElement> CreateElements()
        {
            List<XElement> list = new List<XElement>();

            for (int i = 1; i < 100; i++)
            {
                XElement itemElement = new XElement("item",
                                            new XElement("title", i),
                                            new XElement("link", "http://code-inside.de")
                                       );
                list.Add(itemElement);
            }

            return list;
        }

That´s it :)

[ Download Source Code ]

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

About the author

Written by Code Inside Team

Currently there is no additional info about this author.

3 Responses

  1. vLinq is really Great Tool. By using this tool easily create linq query.

    I came to know about this tool from
    the blog http://www.elevatesoftsolutions.in/post/2008/11/01/LINQ-Query-Builder-Tools.aspx

    In this blog one more LINQ Query Builder Tool is mentioned that is LinqPad.

    Some demerits are in LinqPad that I came to know after using it. Thus I prefer to use vLinq. Good thing about this that It add-in the VS and query generate can be reused.

    Really Great Tool


    KUnal MEhta

    The Blog have Fun n FUN
    http://360by2.blogspot.com

    Reply

Comment on this post

Recent Posts

  • image1452_thumb.png
    Javascript to Dart Translator

      Dart, a Google Javascript alternative was presented a few months ago and the web developer scene are a little bit unsure about the usability of Dart. To declare the language Google has translated the Javascript basics into Dart. The result is this “Translator”. In my opinion the name doesn’t find that well because it’s ...

  • image1366-570x194.png
    Twitter Bootstrap as UI-kit

      HTML and CSS are not foreign words for me but I regret, I’m not a Web designer – I see myself as a webdeveloper. But at least a dressy side is a must. But thank good there are some ready “Systems”. Twitter Bootstrap Twitter Bootstrap is a Toolkit for every kind of Web applications. ...

  • image1441.png
    Fix: the value ‚x‘ is not valid for Foo in ASP.NET MVC

      To get files into the MVC Controller Modelbinding from MVC is a clever method. But in fact it is a little bit complicated to set the error message if the connection failed. Example: public class RegisterModel { ... [Required] [DataType(DataType.EmailAddress)] [Display(Name = "Email address")] public string Email { get; set; } [Required] [Display(Name = ...

  • You Tube API – recall Video Meta files with .NET

      A loooong time ago I’ve blogged about how to access to You Tube with the Google Data APIs. After all that time there are several new ways how to recall files. Google offers You Tube a “simple” surface. If you prefer to do low-Level HTTP calls it is also possible. Aim: I want the ...

  • image1426-570x194.png
    MacBook Pro for .NET Developer – useful ore just pretty?

      I own a MacBook Pro (from 2010) for about a year now and because I’ve used to think about this Question since I have it, I’m going to blog about my experience now. My Notebook Configurations MacBook Pro April 2010 - 2,66 Intel Core i7 - 8GB RAM - 15’’ Glossy Display - Intel ...

Support us