Today I needed to save a fully populated dataset as xml to my drive in order to simplfly unit testing. I thought this would be pretty straight forward, but as it turns out it was not. There is one little wrinkle that may bite you if you are not careful. Below I will describe what I tried and why it failed and finally what worked.
Problem: I need to save a fully populated Dataset as XML to disk
Solution Attempt 1: Use DataSet.WriteXml( LocationOnDisk )
This worked, mostly. I was able to get the XML document and reload it into a a dataset using DataSet.ReadXml( LocationOnDisk ). A problem arose because one of my columns was always null. And by default simply doing a WriteXml does not save schema information. More information on this here.
Solution Attempt 2: Use DataSet.WriteSchema (LocationOnDisk) then user DataSet.WriteXml( LocationOnDisk)
This worked. By saving my schema information for the dataset I am later able to repopulate the data and have my null values replaced. In order to do this all you have to do is the following.
DataSet ds = new DataSet();
ds.ReadXmlSchema( XsdFileLocation );
ds.ReadXml( XmlFileLocation );
I know this seems pretty trival, but If you do not understand how Xml and schemas work you could spin your wheels for a while trying to figure out why your column/data was not repopulated.
Hope this helps someone,
Till next time,
[----- Remember to check out DimeCasts.Net -----]
10-06-2008 3:36 PM