I Think I Love LINQ

I am beggining to really like to ease of using LINQ, On a project I am working on now I am storing configuration in an XML file like so:

<sites>
    <site id="1" sitename="foo">
       <domains>
          <domain url="www.foo.com" />
       </domains>
       <settings>
          <setting key="foo" value="foo" />
       </settings>
    </site>
</sites>

My original solution to load this involded looping over the nodes looping over the domains and settings and loading this into my Site object. Time consuming to write and looked messy. And then LINQ to XML steps in. My code to parse this XML file into my collection of Site objects now becomes:

Me.Sites = (From site In document.Descendants("site") _
                        Select New Site With { _
                        .ID = site.Attribute("id").Value, _
                        .SiteName = site.Attribute("sitename").Value, _
                        .Domains = (From domain In site.Elements("domains").Descendants("domain") _
                                    Select domain) _
                                    .ToDictionary(Function(domain) domain.Attribute("url").Value, Function(domain) domain.Attribute("url").Value), _
                        .Settings = (From setting In site.Elements("settings").Descendants("setting") _
                                    Select setting) _
                                    .ToDictionary(Function(setting) setting.Attribute("key").Value, Function(setting) setting.Attribute("value").Value) _
                        }).ToList()



The speed of this is actually pretty fast and I tested with some large XML files and was happy with the performance. And I dont think life can get easier....


Cheers

Stefan
 

Published Monday, December 24, 2007 10:51 AM by stefan.sedich

Comments

# links for 2007-12-24 &laquo; dstelow notes&#8230;

Monday, December 24, 2007 6:36 PM by links for 2007-12-24 « dstelow notes…

Pingback from  links for 2007-12-24 &laquo; dstelow notes&#8230;

# Link Listing - December 28, 2007

Saturday, December 29, 2007 12:08 AM by Christopher Steen

Announcements Secret Server 4.0 has shipped! [Via: thycotic ] Christmas present - BlogEngine.NET 1.3...

# Link Listing - December 28, 2007

Saturday, December 29, 2007 12:08 AM by Christopher Steen

Link Listing - December 28, 2007

# My Domains &raquo; Blog Archive &raquo; I Think I Love LINQ

Thursday, January 03, 2008 2:58 AM by My Domains » Blog Archive » I Think I Love LINQ

Pingback from  My Domains  &raquo; Blog Archive   &raquo; I Think I Love LINQ

# re: I Think I Love LINQ

Monday, January 28, 2008 1:08 PM by RagingKore

That's because you dont know that Subsonic exists...

http://subsonicproject.com/

Leave a Comment

(required) 
(required) 
(optional)
(required)