469,349 Members | 1,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,349 developers. It's quick & easy.

Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to convert
that array to a dataset. So I serialize the array to XML, then try to load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional info
is "System Error". Does anyone know the best way to do this? I prefer not
to write to disk. Thanks for any advice!

--Marty
Nov 12 '05 #1
2 8488
Did you try to "rewind" the MemoryStream?

After you serialized the objects to XML, the stream is positioned at
right behind the last character.

In order to read the contents of the stream into the DataSet you need to
position the stream at the beginning of the content:
MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
mem.Position = 0;

Should do the trick.

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko

-----Original Message-----
From: Marty McDonald [mailto:mc******@wsdot.wa.gov]
Posted At: Tuesday, November 09, 2004 7:02 PM
Posted To: microsoft.public.dotnet.xml
Conversation: Array to Xml to DataSet
Subject: Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to convert that array to a dataset. So I serialize the array to XML, then try to
load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional info is "System Error". Does anyone know the best way to do this? I prefer not
to write to disk. Thanks for any advice!

--Marty

Nov 12 '05 #2
I was not resetting the memory pointer, I did not know I needed to do that.
I used your suggestion, and now the code works perfectly.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
mem.Position = 0; <=== I added this
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r);

Thanks very much Christoph!
--Marty

"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:O0**************@TK2MSFTNGP12.phx.gbl...
Did you try to "rewind" the MemoryStream?

After you serialized the objects to XML, the stream is positioned at
right behind the last character.

In order to read the contents of the stream into the DataSet you need to
position the stream at the beginning of the content:
MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);


mem.Position = 0;

Should do the trick.

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko

-----Original Message-----
From: Marty McDonald [mailto:mc******@wsdot.wa.gov]
Posted At: Tuesday, November 09, 2004 7:02 PM
Posted To: microsoft.public.dotnet.xml
Conversation: Array to Xml to DataSet
Subject: Array to Xml to DataSet

I have an array of objects (my own custom class). I'm trying to

convert
that array to a dataset. So I serialize the array to XML, then try to
load
the XML to a dataset.

MemoryStream mem = new MemoryStream();
XmlTextWriter w = new XmlTextWriter(mem,System.Text.Encoding.UTF8);
ser.Serialize(w,books);
XmlTextReader r = new XmlTextReader(mem);
DataSet dsBooks = new DataSet();
dsBooks.ReadXml(r); <=== error here

However, the last line generates System.Xml.XmlException. Additional

info
is "System Error". Does anyone know the best way to do this? I

prefer
not
to write to disk. Thanks for any advice!

--Marty


Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by EMiller | last post: by
6 posts views Thread by Carl | last post: by
6 posts views Thread by Michael Rodriguez | last post: by
7 posts views Thread by Jed | last post: by
5 posts views Thread by Geoff Jones | last post: by
1 post views Thread by elziko | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.