How to Load an Xml C# String into a DataSet

  • Thread starter Thread starter Edward Mitchell
  • Start date Start date
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
 
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
 
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
 
Back
Top