Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

LINQ to Web 2.0


LINQ-to-XML

What LINQ-to-SQL does for the world of relational database integration, LINQ-to-XML does for the world of XML data access and manipulation. Using the same LINQ-style syntax that works on databases, you can create powerful queries that parse through XML documents and retrieve data in the exact shape you like—whether that shape is in the form of dynamically generated C# classes or simply another XML schema.

LINQ-to-XML includes a new set of XML-related classes such as XDocument, XElement, and XAttribute. The beauty of these classes is that their constructors take instances of applicable child classes. This lets you create elements on-the-fly in a nested constructor format like this:

XElement newElement = 
   new XElement( "data",       new XAttribute("value", "12")); 

This creates an element that looks like:

<data value="12"/> 

This is a more intuitive and easy-to-use syntax than the existing DOM classes in the .NET Framework when you are specifically dealing with dynamic query and production of XML data. In the Moniqer project, we used LINQ-to-XML to dynamically create RSS feeds from the output of LINQ-to-SQL queries in the same line of code. When the code itself becomes a thing of beauty, you know you're on the right path. Listing Three, for instance, takes bookmarks contained in the bookmarkList parameter, which is of type IQueryable<BookmarkResults> and wraps that in a custom-built XML package that is in a format readable by most RSS readers.

Response.ContentType = "text/xml"; 
XElement rssRoot = new XElement("rss", 
   new XAttribute("version", "2.0"), 
   new XElement("channel", 
      new XElement("title", feedTitle), 
      new XElement("link", "http://[my server]/moniqer/default.aspx/"), 
      new XElement("description", feedDescription), 
      new XElement("ttl", "360"), 
      from bookMark in bookmarkList.Take(rowLimit)  
         select new XElement("item", 
            new XElement("title", bookMark.Bookmark.Title), 
            new XElement("link", bookMark.Bookmark.Address), 
            new XElement("pubDate", bookMark.Tags.Last().
                                      CreatedOn.ToString()+ " GMT"), 
            new XElement("Creator", bookMark.Tags.Last().UserID), 
            new XElement("author", bookMark.Tags.Last().UserID), 
            new XElement("description", bookMark.Bookmark.Description), 
            from tag in bookMark.Tags 
               select new XElement("category", tag.Title))); 
Response.Write(rssRoot.ToString()); 
Response.End(); 
Listing Three

The Future of ADO.NET

ADO.NET's future is tied to ADO.NET vNext, which uses LINQ to support the ADO.NET Entity Framework—an abstraction model that lets you define an entity model in an XML file. Underneath that there is a schema definition XML file that corresponds directly to the table structure in your database. There is also a mapping file that relates the Entity Model with the physical schema. This abstraction and loose coupling provides an amazing framework that supports things like Entity Inheritance and Entity Composition (creating a single C# object class/entity that reads and writes from multiple tables). From what I can tell, the ADO.NET team has done a great job of taking the best ideas that drove ObjectSpaces, WinFS, and LINQ-to-SQL and honing them into what I think is a glimpse at the future of data access for enterprise applications.

The Future of Moniqer

The current version of Moniqer resides on CodePlex (www.codeplex.com/Wiki/View .aspx?ProjectName=Moniqer). We are working on rewriting the Moniqer data tier in the Entity Framework to provide the rich scalability, flexibility, and performance for our enterprise applications. Once that is complete, we can easily add schema-changing features such as support for named groups and scoping of bookmarks, tag blending (monitoring someone else's tag within your own), discussions revolving around individual bookmarks, and more.

In short, following CSS design patterns using the ASP.NET web application framework and backing that framework with SQL Server 2005 and LINQ-to-SQL provides you with a powerful foundation on which to build enterprise social bookmarking and similar applications. If you are planning on building a data-driven application with ASP.NET, take a look at LINQ-to-SQL and ADO.NET vNext and check out the source code for Moniqer.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.