How to Load an Xml C# String into a DataSet

E

Edward Mitchell

I have a string containing the XML text of my database. How can I create a
data set from this String?

The simple way would be to convert the String to a memory stream and then
read that memory stream using DataSet.ReadXml(...). However I can't find a
way to create a MemoryStream from a String. One of the constructors for
MemoryStream needs a byte[] array. I don't know how to get this out of the
string. I can get a char[] array but that's not compatible with a byte[]
array.

This is the sample code I was trying to use:

String sClassesXml = (String) Session["ClassesXml"];
// doesn't work!!!
byte[] baClassesXml = sClassesXml.ToCharArray();
MemoryStream ms = new MemoryStream(baClassesXml);
DataSet ds = new DataSet();
ds.ReadXml(ms);


It would be easy in C++!

Ed
--
Edward E.L. Mitchell
Web: www.racesail.org
Phone: (239)415-7039
6707 Daniel Court
Fort Myers, FL 33908
 
C

Cor Ligthert

Edward,

To serialize/deserialize a dataset you don't need directly a memorystream,
maybe you can try this one.
In this message changed from VBNet to C# so watch typos

Serialize
\\\\
System.IO.StringWriter sw = New System.IO.StringWriter();
ds.WriteXml(sw);
string mystring = sw.tostring();
///
Deserialize
\\\
System.IO.StringReader sr = new System.IO.StringReader(mystring);
DataSet ds2 = new DataSet();
ds2.ReadXml(sr);
///
I hope this helps a little bit?

Cor
 
E

Edward Mitchell

Cor,

That worked like a charm. I hadn't noticed the overload for Read/WriteXml
that took a TextWriter object. I was focused on the stream argument since I
know how to change a string to a stream in C++!.

Ed
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top