BUG? DataSet.ReadXML with MemoryStream

  • Thread starter Thread starter Chuck Haeberle
  • Start date Start date
C

Chuck Haeberle

We're using the Microsoft Application Blocks SQL Helper in our projects,
which is a great time saver but doesn't support use of Strongly Typed
DataSets.

I'm trying to find an easy way to take a generic dataset which has been
populated and transfer its data into my strongly typed data set.

Assume an XSD called FileList. I use a derived class based on the XSD since
changing the XSD regenerates its base code...

public class FileListHelper : FileList
{
....
public void SetData(DataSet data)
{
MemoryStream ms = new MemoryStream();
data.WriteXml(ms);
this.ReadXml(ms);
}
}

Now reading the overloads for ReadXML this should work, but instead I get an
error that the Root Node doesnt' exist on the ReadXML.

Any advice? Or alternate approches for taking a DataSet and moving its data
into an XSD or XSD Derivative?
 
Chuck,

After you do the WriteXml to the Stream your stream will
be positioned at its end.

You have to move the Memory stream back to the beginning
by setting its Position property before you can write it
out. Try changing you code to the following.

public class FileListHelper : FileList
{
.....
public void SetData(DataSet data)
{
MemoryStream ms = new MemoryStream();
data.WriteXml(ms);
// Move the stream back to the beginning
ms.Position = 0;
this.ReadXml(ms);
}
.....
}

Took me hours of tearing my hair out to work out that
one. If your trying to do anything complex with Xml in a
DataSet there are quite a few problems to get around,
although when you get it working its really cool! I have
a couple of posts in this group with issues I have been
unable to resolve, if you sort any of them out be sure to
let me know. Hope this helps.

Regards,

David
 
Back
Top