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