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


Posted 10-06-2008 3:36 PM by Derik Whittaker
Filed under:

[Advertisement]

Comments

Dan Sniderman wrote re: Save Dataset contents to XML and Null Values
on 10-07-2008 8:06 AM

Datasets are the root of much evil...

Derik Whittaker wrote re: Save Dataset contents to XML and Null Values
on 10-07-2008 8:12 AM

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

Dew Drop - October 7, 2008 | Alvin Ashcraft's Morning Dew wrote Dew Drop - October 7, 2008 | Alvin Ashcraft's Morning Dew
on 10-07-2008 9:35 AM

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

Arjan`s World » LINKBLOG for October 7, 2008 wrote Arjan`s World » LINKBLOG for October 7, 2008
on 10-07-2008 10:14 AM

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

Michael C. Neel wrote re: Save Dataset contents to XML and Null Values
on 10-11-2008 8:19 PM

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.

Derik Whittaker wrote re: Save Dataset contents to XML and Null Values
on 01-18-2009 10:26 PM

@Michael,

Not for this project, but I have used it in the past.

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)