DataSet.WriteXML to stream

  • Thread starter Thread starter Chris Wagner
  • Start date Start date
C

Chris Wagner

Is there a way to writeXML to a stream (MemoryStream maybe)? I don't want to
write to a xml file, just to the stream so I can convert it to a string.
Thanks for your help.
 
Yes .. it is possible, and here is how ..

[STAThread]
static void Main(string[] args)
{
DataSet ds = new DataSet() ;
DataTable dt = new DataTable() ;
DataColumn dc = new DataColumn("Sahil") ;
dt.Columns.Add(dc) ;
DataRow dr = dt.NewRow() ;
dr[0] = "Loves .NET" ;
dt.Rows.Add(dr) ;
ds.Tables.Add(dt) ;


MemoryStream ms = new MemoryStream() ;
ds.WriteXml(ms) ;
Console.WriteLine(System.Text.Encoding.ASCII.GetString(ms.ToArray())) ;
}

- Sahil Malik
Independent Consultant
You can reach me thru my blog at -
http://www.dotnetjunkies.com/weblog/sahilmalik/
 
Hi Chris,

As an alternative for Sahil,

I have as well thought for a long time it should be done using the
memstream, but there is no need for it.

Serialize
\\\\
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw)
Dim mystring As String = sw.tostring
///
Deserialize
\\\
Dim sr As New System.IO.StringReader(mystring)
Dim ds2 As New DataSet
ds2.ReadXml(sr)
///

I hope this helps a little bit?

Cor
 
Maybe this will help...


Shared Function SerializeThingToXmlString(ByVal thing As Object) As
String
Dim stringWriter As StringWriter = New StringWriter()
Dim serializer As XmlSerializer = New XmlSerializer(thing.GetType())
serializer.Serialize(stringWriter, thing)
Return stringWriter.ToString()
End Function

Shared Function SerializeThingToXmlTextReader(ByVal thing As Object) As
XmlTextReader
Dim ms As MemoryStream = New MemoryStream()
Dim serializer As XmlSerializer = New XmlSerializer(thing.GetType())
serializer.Serialize(ms, thing)
ms.Seek(0, SeekOrigin.Begin)
Return New XmlTextReader(ms)
End Function
 
Chris,
In addition to the others comments.

If you look at the DataSet.WriteXml overloads in help:

http://msdn.microsoft.com/library/d.../frlrfSystemDataDataSetClassWriteXmlTopic.asp

You will see that it is overloaded to write to:
- String - which is a file name
- Stream - which can be any stream object (FileStream, MemoryStream,
NetworkStream)
- TextWriter - which can be any TextWriter object (StreamWriter or
StringWriter)
- XmlWriter - Which can be any XmlWriter object (XmlTextWriter)

The DataSet.ReadXml is overloaded in a similar fashion to allow reading from
any of the above's "equivalents" (StreamReader instead of StreamWriter...)

Hope this helps
Jay
 
Chris,
Oh! I prefer the StringWriter.

However based on another project writing XML to "memory", the StringWriter
limits you to Unicode (UTF-16) encoding in the XML.

If you want a different encoding (UTF-8 for example), then you need to use a
MemoryStream, and a StreamWriter with the encoding of your choice.

Hope this helps
Jay
 
Well I tried :)



Jay B. Harlow said:
Chris,
Oh! I prefer the StringWriter.

However based on another project writing XML to "memory", the
StringWriter
limits you to Unicode (UTF-16) encoding in the XML.

If you want a different encoding (UTF-8 for example), then you need to use
a
MemoryStream, and a StreamWriter with the encoding of your choice.

Hope this helps
Jay
 
Jay B. Harlow said:
Oh! I prefer the StringWriter.

However based on another project writing XML to "memory", the StringWriter
limits you to Unicode (UTF-16) encoding in the XML.

Only if you use the default StringWriter.

Here's a little class which derives from StringWriter and allows you to
set the notional encoding at construction:

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}
}
 
Jon,
Doh!

I saw the Encoding property on StringWriter, I did not think to actually
override it with my own, as my project can live with UTF-16 so I did not
really pursue other options...

Thanks for the tip!

Jay
 
Jay B. Harlow said:
Jon,
Doh!

I saw the Encoding property on StringWriter, I did not think to actually
override it with my own, as my project can live with UTF-16 so I did not
really pursue other options...

Thanks for the tip!

It's really a "Doh!" on the part of MS, in my view - why on earth
didn't they provide a constructor for it in the first place?
 
Jon,
It's really a "Doh!" on the part of MS, in my view - why on earth
didn't they provide a constructor for it in the first place?
Totally agree! Maybe they can sneek it in on .NET 2.0.

Jay
 
Hi all,
I went off to do some other emergency calls. Didn't have time to look a
this.
This is great! Thanks all for the help.

Chris
 
Back
Top