现在我正在使用 XmlTextWriter 将 MemoryStream 对象转换为字符串。但我想知道是否有更快的方法将内存流序列化为字符串。
我按照此处给出的代码进行序列化 - http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp
已编辑
串流到字符串
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
string content = sr.ReadToEnd();
SaveInDB(ms);
}
串流
string content = GetFromContentDB();
byte[] byteArray = Encoding.ASCII.GetBytes(content);
MemoryStream ms = new MemoryStream(byteArray);
byte[] outBuf = ms.GetBuffer(); //error here
最佳答案
using(MemoryStream stream = new MemoryStream()) {
stream.Position = 0;
var sr = new StreamReader(stream);
string myStr = sr.ReadToEnd();
}
使用MemoryStream(byte[])时不能使用GetBuffer构造函数。
MSDN 引用:
This constructor does not expose the underlying stream. GetBuffer throws UnauthorizedAccessException.
你必须使用这个 constructor并设置 publiclyVisible = true
以使用 GetBuffer
关于c# - 将内存流对象序列化为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6161453/