Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Little nugget of simplification when loading an XDocument

Today I needed to load in an XML file into memory for later use. At first I was going to use the XmlDocument library (old habits die hard), but later switched over to the XDocument library.  Why, not because one is better (ok, so I think that XDocument is better, but not the point here), but because it caused me less friction (which I guess does make it better).

Consider this need.  I have a list of XML files on disk and I would like to convert them to a list of XML documents. 

Below is my original code using the XmlDocument library:

...

IList returnDocument = new List< XDocument >();
foreach ( string file in files )
{
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load( file );
	returnDocument.Add( xmlDocument );
}

...

Below is my new code using the XDocument library:

....
IList returnDocument = new List< XDocument >();

foreach ( string file in files )
{
	returnDocument.Add( XDocument.Load( file ) );
}
....

Now there is no real difference in what each example does, but there is a difference in how I have to accomplish my need. 

Using the XmlDocument library I need to create an instance, load the instance and finally put it into my return array.

Using the XDocument library, I can simply use the static Load to accomplish this in a single line.

+1 for less code.

Till next time,

[----- Remember to check out DimeCasts.Net -----]


Published Jul 02 2008, 09:53 AM by Derik Whittaker
Filed under: , ,

Comments

Derik Whittaker said:

@sd,

This code will produce a compile error as the load is a void method.  Does not return the xmldocument object.

# July 2, 2008 5:44 PM

sergiopereira said:

Must.. resist.. comment... gaaaaaaa:

If your "files" is a List of strings:

IList returnDocument = files.ConvertAll(f => XDocument.Load(f));

If it's an array:

var docs = Array.ConvertAll(files, f => XDocument.Load(f));

IList returnDocument = new List<XDocument>(docs);

# July 2, 2008 6:29 PM

Derik Whittaker said:

@Sergio

I love that...

And that goes to prove my point even further that with XDocument it saves code.

# July 2, 2008 6:34 PM

Christopher Bennage said:

...and if you love Linq (and you know I do):

var docs = from file in files

                   select XDocument.Load(file);

* With the added bonus of deferred execution!

# July 2, 2008 10:10 PM

Christopher Bennage said:

Hmm, perhaps I should have said "If you love the syntactic sugar of query expressions..."

# July 2, 2008 10:14 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 3, 2008 8:50 AM

Christopher Steen said:

Link Listing - July 6, 2008

# July 7, 2008 3:06 AM

Christopher Steen said:

Sharepoint Having WSPBuilder exclude your dll when creating a .wsp [Via: Sahil Malik ] WPF TemplateBinding:...

# July 7, 2008 3:08 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News