如果我给了一个MemoryStream,我知道已经填充了一个字符串,我如何得到一个字符串回来?
当前回答
byte[] array = Encoding.ASCII.GetBytes("MyTest1 - MyTest2");
MemoryStream streamItem = new MemoryStream(array);
// convert to string
StreamReader reader = new StreamReader(streamItem);
string text = reader.ReadToEnd();
其他回答
这个示例展示了如何从MemoryStream中读取字符串,其中我使用了序列化(使用DataContractJsonSerializer),将字符串从某个服务器传递到客户端,然后,如何从作为参数传递的字符串中恢复MemoryStream,然后,反序列化MemoryStream。
我使用了不同帖子的一部分来执行这个示例。
希望这能有所帮助。
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading;
namespace JsonSample
{
class Program
{
static void Main(string[] args)
{
var phones = new List<Phone>
{
new Phone { Type = PhoneTypes.Home, Number = "28736127" },
new Phone { Type = PhoneTypes.Movil, Number = "842736487" }
};
var p = new Person { Id = 1, Name = "Person 1", BirthDate = DateTime.Now, Phones = phones };
Console.WriteLine("New object 'Person' in the server side:");
Console.WriteLine(string.Format("Id: {0}, Name: {1}, Birthday: {2}.", p.Id, p.Name, p.BirthDate.ToShortDateString()));
Console.WriteLine(string.Format("Phone: {0} {1}", p.Phones[0].Type.ToString(), p.Phones[0].Number));
Console.WriteLine(string.Format("Phone: {0} {1}", p.Phones[1].Type.ToString(), p.Phones[1].Number));
Console.Write(Environment.NewLine);
Thread.Sleep(2000);
var stream1 = new MemoryStream();
var ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
Console.Write("JSON form of Person object: ");
Console.WriteLine(sr.ReadToEnd());
Console.Write(Environment.NewLine);
Thread.Sleep(2000);
var f = GetStringFromMemoryStream(stream1);
Console.Write(Environment.NewLine);
Thread.Sleep(2000);
Console.WriteLine("Passing string parameter from server to client...");
Console.Write(Environment.NewLine);
Thread.Sleep(2000);
var g = GetMemoryStreamFromString(f);
g.Position = 0;
var ser2 = new DataContractJsonSerializer(typeof(Person));
var p2 = (Person)ser2.ReadObject(g);
Console.Write(Environment.NewLine);
Thread.Sleep(2000);
Console.WriteLine("New object 'Person' arrived to the client:");
Console.WriteLine(string.Format("Id: {0}, Name: {1}, Birthday: {2}.", p2.Id, p2.Name, p2.BirthDate.ToShortDateString()));
Console.WriteLine(string.Format("Phone: {0} {1}", p2.Phones[0].Type.ToString(), p2.Phones[0].Number));
Console.WriteLine(string.Format("Phone: {0} {1}", p2.Phones[1].Type.ToString(), p2.Phones[1].Number));
Console.Read();
}
private static MemoryStream GetMemoryStreamFromString(string s)
{
var stream = new MemoryStream();
var sw = new StreamWriter(stream);
sw.Write(s);
sw.Flush();
stream.Position = 0;
return stream;
}
private static string GetStringFromMemoryStream(MemoryStream ms)
{
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
return sr.ReadToEnd();
}
}
}
[DataContract]
internal class Person
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public DateTime BirthDate { get; set; }
[DataMember]
public List<Phone> Phones { get; set; }
}
[DataContract]
internal class Phone
{
[DataMember]
public PhoneTypes Type { get; set; }
[DataMember]
public string Number { get; set; }
}
internal enum PhoneTypes
{
Home = 1,
Movil = 2
}
}
以前的解决方案在涉及编码的情况下不起作用。这是一个“现实生活”的例子,如何正确地做到这一点…
using(var stream = new System.IO.MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(IEnumerable<ExportData>), new[]{typeof(ExportData)}, Int32.MaxValue, true, null, false);
serializer.WriteObject(stream, model);
var jsonString = Encoding.Default.GetString((stream.ToArray()));
}
只使用方法Convert。ToBase64String
Convert.ToBase64String(inputStream.ToArray());
使用StreamReader,然后你可以使用ReadToEnd方法返回一个字符串。
为什么不在MemoryStream类型上做一个很好的扩展方法呢?
public static class MemoryStreamExtensions
{
static object streamLock = new object();
public static void WriteLine(this MemoryStream stream, string text, bool flush)
{
byte[] bytes = Encoding.UTF8.GetBytes(text + Environment.NewLine);
lock (streamLock)
{
stream.Write(bytes, 0, bytes.Length);
if (flush)
{
stream.Flush();
}
}
}
public static void WriteLine(this MemoryStream stream, string formatString, bool flush, params string[] strings)
{
byte[] bytes = Encoding.UTF8.GetBytes(String.Format(formatString, strings) + Environment.NewLine);
lock (streamLock)
{
stream.Write(bytes, 0, bytes.Length);
if (flush)
{
stream.Flush();
}
}
}
public static void WriteToConsole(this MemoryStream stream)
{
lock (streamLock)
{
long temporary = stream.Position;
stream.Position = 0;
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8, false, 0x1000, true))
{
string text = reader.ReadToEnd();
if (!String.IsNullOrEmpty(text))
{
Console.WriteLine(text);
}
}
stream.Position = temporary;
}
}
}
当然,在将这些方法与标准方法结合使用时要小心。:)……为了实现并发性,你需要使用那个方便的streamLock。
推荐文章
- 是否有可能更新一个本地化的故事板的字符串?
- 为什么字符串类型的默认值是null而不是空字符串?
- 在Python中包装长行
- AutoMapper:“忽略剩下的?”
- 如何找出一个文件存在于c# / .NET?
- string. isnullorempty (string) vs. string. isnullowhitespace (string)
- 完全外部连接
- 如何检查字符串的特定字符?
- Haskell:将Int转换为字符串
- 如何使用。net 4运行时运行PowerShell ?
- 在foreach循环中编辑字典值
- 使用System.IO.Compression在内存中创建ZIP存档
- 将字符串转换为Uri
- jUnit中的字符串上的AssertContains
- 在WPF中引入一个窗口到前面