how do I write the output of a serialized class as a string?

  • Thread starter Thread starter Tarren
  • Start date Start date
T

Tarren

Hi:

My serialized class is ending up in a XML.XMLTEXTRIDER

How do I write from that to a string? It seems as if it is storing it as a
stream? How do I read the contents of a stream into a string?

Thanks!
 
Tarren said:
My serialized class is ending up in a XML.XMLTEXTRIDER

I don't know if that's incoming (XmlTextReader) or outgoing
(XmlTextWriter)?

If you are holding an XmlTextReader named ''reader,''
then you can read the XML into a string like this,

Dim xmlStr As String
Dim doc As XmlDocument = New XmlDocument( )
' Uncomment next statement if you care about whitespace.
' doc.PreserveWhitespace = True
doc.Load( reader)
xmlStr = doc.OuterXml

If you are going to hold an XmlTextWriter named ''writer,'' then wrap
it around a System.IO.StringWriter before you start serializing an
object named ''myObject'' to an XmlSerializer named 'serializer'',

Dim xmlStr As String
Dim strWriter As System.IO.StringWriter

strWriter = New System.IO.StringWriter( New System.Text.StringBuilder( ) )
writer = New XmlTextWriter( strWriter )
serializer.Serialize( writer, myObject)
xmlStr = strWriter.ToString( )
How do I read the contents of a stream into a string?

Wrap the Stream in a System.IO.StreamReader and then call
ReadToEnd( ),

Dim streamStr As String
streamStr = New System.IO.StreamReader( stream).ReadToEnd( )


Derek Harmon
 
* "Tarren said:
My serialized class is ending up in a XML.XMLTEXTRIDER

How do I write from that to a string? It seems as if it is storing it as a
stream? How do I read the contents of a stream into a string?

Classes 'StreamWriter' or, more low-level, 'MemoryStream' +
'System.Text.Encoding.GetString'.
 
thanks all! I ended up using a MemoryStream

Most of the examples I had seen involved reading from a .xml file, and so
those examples use a FileStream
but MemoryStream did the trick.

:)
 
Hi Tarren,

I did not notice this thread, the memorystream does it. However the
stringwritter does it mostly more easy.

A sample
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)
///

Sorry that I missed the thread, however the memorystream does it with the
streamreader also, but you need an extra step for that.

Cor
 
Back
Top