The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








Latest Diggs - Programming

Internet Blogs - Blog Top Sites

Site Hits

Total: 2,551,710
since: 19 Jan 2005

Query Data and Generate an RSS Feed in the same XLinq/DLinq Query!

posted Sun 21 May 06

In previous blog entries on LINQ and lambdas, I illustrated the basics of querying in-memory objects using LINQ and how it is all made possible through the use of language extensions and lambda functions. One of the other things that is made possible through LINQ and lambdas is XLinq, an extension to the C# 3.0 language that allows developers to query XML documents in much the same way they can now query database elements and in-memory enumerable objects such as lists and collections.

In addition to being able to query XML documents, you can also generate XML elements on the fly within a LINQ query by creating an instance of the XElement or XAttribute class. In the sample below, I create a complete, fully-functioning RSS feed using a single LINQ query! The data structure from the sample below involves three tables: Articles, Categories, and ArticleCategories. To generate an RSS feed of all of the articles contained in the database, as well as their categories and sort them all by creation date in descending order, all I have to do is use the following LINQ query:

TestDB testDatabase = new TestDB(
    "server=localhost; initial catalog=TestDB; integrated security=SSPI;");

XElement rssRoot = new XElement("rss",
    new XAttribute("version", "2.0"),
    new XElement("channel",
        new XElement("title", "My RSS Feed"),
        new XElement("link", "
http://dotnetaddict.dotnetdevelopersjournal.com"),
        new XElement("description", "This is my RSS Feed"),
        from article in testDatabase.Articles
        orderby article.CreatedOn descending
        select new XElement("item",
            new XElement("title", article.Title),
            new XElement("link", "article.aspx?id="+article.ID.ToString()),
            new XElement("description", article.Description),
            from articleCategory in article.ArticleCategories
                select new XElement("category", articleCategory.Categories.Description)
             )
         )
    );

The TestDB class refers to a class that was generated by absorbing the database schema from the TestDB database and converting that schema into attribute-decorated business objects by the SQLMetal tool that comes with the LINQ preview. The code above produces the following output when there is a single 4-category article contained in the database:

<rss version="2.0">
  <channel>
    <title>My RSS Feed</title>
    <link>http://dotnetaddict.dotnetdevelopersjournal.com</link>
    <description>This is my RSS Feed</description>
    <item>
      <title>New Article</title>
      <link>article.aspx?id=1</link>
      <description>This is a new article</description>
      <category>new</category>
      <category>category</category>
      <category>sample</category>
      <category>test</category>
    </item>
  </channel>
</rss>

At this point there's very little left to say. Granted, creating an RSS feed isn't the most difficult of tasks, but it typically involves painstaking and tedious DOM manipulation or the creation of a separate tool class that encapsulates the manipulation of RSS-specific XML elements. The fact that you can blend the query to retrieve the data that will be in the RSS feed with the code that generates the RSS XML itself is extremely powerful, and applies to far more tasks than just RSS generation.

If you think of the number of times in the past year or two that you've had to read data and build an XML hierarchy from that data (and you didn't cheat and use SQL 2005's XQuery support), then you can probably picture how much time this little technique would have saved you.

tags:              

links: digg this    del.icio.us    technorati    reddit




Tag Related Posts

MobileMe vs. Live Mesh Throwdown - Round 1

Wed 16 Jul 08 10:33 A GMT-05

Building Model Classes in C# and Cocoa

Sun 15 Jun 08 3:13 P GMT-05
tags:            

NYC SharePoint Developer Needed

Mon 12 May 08 12:09 P GMT-05

CLINQ v1.1.0.0 Released!

Fri 02 May 08 5:38 P GMT-05
tags:          

One Framework to Rule them All

Mon 25 Feb 08 6:49 P GMT-05

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Unexpected and Unsafe behavior in LINQ

Wed 14 Nov 07 8:09 P GMT-05
tags:    

Continuous LINQ - Can I write games with it?

Mon 13 Aug 07 3:09 P GMT-05
tags:        

Continuous LINQ

Mon 06 Aug 07 1:21 P GMT-05
tags:    

Dynamic, Observable LINQ Views

Tue 31 Jul 07 1:21 A GMT-05
tags:        

The dreaded language bleed-over has begun

Tue 19 Jun 07 6:23 P GMT-05
tags:        

Installing Orcas Beta 1 - VMware Style

Mon 23 Apr 07 12:16 P GMT-05

SuiteTwo Debuts

Thu 19 Apr 07 2:53 P GMT-05

Core Data - Almost too Easy?

Wed 18 Apr 07 2:23 P GMT-05

Will Silverlight be DOA?

Mon 16 Apr 07 8:02 P GMT-05

My Little Pony .NET Unleashed 2007

Fri 30 Mar 07 1:59 P GMT-05

An experience with the Leopard beta

Mon 26 Mar 07 7:45 P GMT-05
tags:                

Authorness

Thu 15 Mar 07 1:44 P GMT-05

Visual Studio "Orcas" - March CTP is Available

Wed 28 Feb 07 12:28 P GMT-05
tags:            

Objective-C Categories vs C# 3.5 Language Extensions

Mon 26 Feb 07 1:05 P GMT-05
tags:                

Localizing a WPF Application

Tue 22 Aug 06 11:39 A GMT-05
tags:            

Is Windows Workflow Foundation Too Complex?

Fri 18 Aug 06 12:15 P GMT-05

DLinq vs the ADO.NET Entity Framework

Fri 23 Jun 06 4:01 P GMT-05

WPF/XAML and LINQ - A match made in heaven

Tue 06 Jun 06 11:32 A GMT-05
tags:                  

Language Extensions in C# 3.0

Wed 31 May 06 2:17 P GMT-05

Lambda Lambda Lambda

Sun 21 May 06 1:01 A GMT-05