Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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 -----]


Posted 07-02-2008 9:53 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

sd wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 5:26 PM

just because....

foreach ( string file in files )  

{  

   returnDocument.Add(new XmlDocument().Load( file ));  

}  

Derik Whittaker wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 5:44 PM

@sd,

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

sergiopereira wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 6:29 PM

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);

Derik Whittaker wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 6:34 PM

@Sergio

I love that...

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

Christopher Bennage wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 10:10 PM

...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!

Christopher Bennage wrote re: Little nugget of simplification when loading an XDocument
on 07-02-2008 10:14 PM

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

DotNetKicks.com wrote Little nugget of simplification when loading an XDocument
on 07-03-2008 8:50 AM

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

Christopher Steen wrote Link Listing - July 6, 2008
on 07-07-2008 3:06 AM

Link Listing - July 6, 2008

Christopher Steen wrote Link Listing - July 6, 2008
on 07-07-2008 3:08 AM

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

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)