Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from memory stream to string

I am trying to write an object to an Xml string and take that string and save it to a DB. But first I need to get the string...

    private static readonly Encoding LocalEncoding = Encoding.UTF8;      public static string SaveToString<T> (T settings)     {         Stream stream = null;         TextWriter writer = null;         string settingsString = null;          try         {             stream = new MemoryStream();              var serializer = new XmlSerializer(typeof(T));              writer = new StreamWriter(stream, LocalEncoding);              serializer.Serialize(writer, settings);              var buffer = new byte[stream.Length];              stream.Read(buffer, 0, (int)stream.Length);                          settingsString = LocalEncoding.GetString(buffer);         }         catch (Exception ex)         {             // If the action cancels we don't want to throw, just return null.         }         finally         {             if (stream != null)                 stream.Close();              if (writer != null)                 writer.Close();         }          return settingsString;     } 

This seems to work, the stream gets filled with bytes. But when I come to read it back into the buffer and then into the string... the buffer is filled with '0'! Not sure what I doing wrong here guys.

like image 914
tigerswithguitars Avatar asked May 04 '12 09:05

tigerswithguitars


People also ask

How do you read a memory stream?

This code shows how to use MemoryStream class and its member to read data in memory stream, which can be used to save it from there. int count; //GetByteData function to get Byte data like if you fetch Image column data from sqlserver or somewhere. byte[] byteArray = getByteData();

How do you turn a stream into a string?

To convert an InputStream Object int to a String using this method. Instantiate the Scanner class by passing your InputStream object as parameter. Read each line from this Scanner using the nextLine() method and append it to a StringBuffer object. Finally convert the StringBuffer to String using the toString() method.

How do I write a string to MemoryStream?

MemoryStream stringInMemoryStream = new MemoryStream(ASCIIEncoding. Default. GetBytes("Your string here")); The string will be loaded into the MemoryStream , and you can read from it.


1 Answers

If you'd checked the results of stream.Read, you'd have seen that it hadn't read anything - because you haven't rewound the stream. (You could do this with stream.Position = 0;.) However, it's easier to just call ToArray:

settingsString = LocalEncoding.GetString(stream.ToArray()); 

(You'll need to change the type of stream from Stream to MemoryStream, but that's okay as it's in the same method where you create it.)

Alternatively - and even more simply - just use StringWriter instead of StreamWriter. You'll need to create a subclass if you want to use UTF-8 instead of UTF-16, but that's pretty easy. See this answer for an example.

I'm concerned by the way you're just catching Exception and assuming that it means something harmless, by the way - without even logging anything. Note that using statements are generally cleaner than writing explicit finally blocks.

like image 117
Jon Skeet Avatar answered Sep 29 '22 06:09

Jon Skeet