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