Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing a memorystream object to string

Right now I'm using XmlTextWriter to convert a MemoryStream object into string. But I wan't to know whether there is a faster method to serialize a memorystream to string.

I follow the code given here for serialization - http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp

Edited

Stream to String

ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
    string content = sr.ReadToEnd();
    SaveInDB(ms);
}

String to Stream

string content = GetFromContentDB();
byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream ms = new MemoryStream(byteArray); 
byte[] outBuf = ms.GetBuffer(); //error here
like image 888
NLV Avatar asked May 28 '11 11:05

NLV


People also ask

What is string serialization?

String serialization is the process of writing a state of object into a byte stream. In python, the “pickle” library is used for enabling serialization. This module includes a powerful algorithm for serializing and de-serializing a Python object structure.


1 Answers

In VB.net i used this

Dim TempText = System.Text.Encoding.UTF8.GetString(TempMemoryStream.ToArray())

in C# may apply

like image 143
Esteban Perez Avatar answered Sep 21 '22 19:09

Esteban Perez