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 ]

kick it on DotNetKicks.com

3 Comments so far »

  1. HowTo: Use PicLens and other MediaRSS Clients for your own Website (create MediaRSS with LINQ to XML) | Code-Inside Blog International said

    am July 1 2008 @ 1:29 pm

    [...] blogpost is related to the "RSS XLINQ" post – but this time the result will be cooler [...]

  2. Recent Links Tagged With "autodiscovery" - JabberTags said

    am October 20 2008 @ 12:49 am

    [...] public links >> autodiscovery HowTo: Create RSS Feeds with LINQ to XML (XLinq) Saved by roncioso on Sat 18-10-2008 iPhone and iPod touch: &quot;Unable to Verify [...]

  3. 360by2 said

    am November 2 2008 @ 8:07 pm

    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

Comment RSS · TrackBack URI

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: