Serializing a memorystream object to string

后端 未结 2 1368
忘了有多久
忘了有多久 2021-01-17 12:14

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.

相关标签:
2条回答
  • In VB.net i used this

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

    in C# may apply

    0 讨论(0)
  • 2021-01-17 12:57
    using(MemoryStream stream = new MemoryStream()) {
       stream.Position = 0;
       var sr = new StreamReader(stream);
       string myStr = sr.ReadToEnd();
    }
    

    You cant use GetBuffer when you use MemoryStream(byte[]) constructor.

    MSDN quote:

    This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.

    You must use this constructor and set publiclyVisible = true in order to use GetBuffer

    0 讨论(0)
提交回复
热议问题