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



Save Dataset contents to XML and Null Values

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 -----]


Published Oct 06 2008, 03:36 PM by Derik Whittaker
Filed under:

Comments

Dan Sniderman said:

Datasets are the root of much evil...

# October 7, 2008 8:06 AM

Derik Whittaker said:

@Dan,

They are the root of many evil, sadly my current application passes them between web service calls (on the list to change) and I needed a way to avoid having to actually make the web service call.

# October 7, 2008 8:12 AM

Dew Drop - October 7, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - October 7, 2008 | Alvin Ashcraft's Morning Dew

# October 7, 2008 9:35 AM

Arjan`s World » LINKBLOG for October 7, 2008 said:

Pingback from  Arjan`s World    » LINKBLOG for October 7, 2008

# October 7, 2008 10:14 AM

Michael C. Neel said:

Did you try using an XMLSerializer (or binary) on the dataset?  I think this will serialize the schema and data in one file.  If you use a binary serializer be sure to set the RemotingFormat to binary on the DataSet before the Serialize() call.

# October 11, 2008 8:19 PM

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

Red-Gate!