c# – 将memorystream对象序列化为字符串

c# – 将memorystream对象序列化为字符串,第1张

概述现在我使用XmlTextWriter将一个MemoryStream对象转换成字符串.但是我不知道是否有更快的方法来将memorystream序列化为字符串. 我按照这里给出的代码序列化 – http://www.eggheadcafe.com/articles/system.xml.xmlserialization.asp 编辑 流到字符串 ms.Position = 0;using (Stre 现在我使用XmlTextWriter将一个MemoryStream对象转换成字符串.但是我不知道是否有更快的方法来将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并设置ublicVisible= true才能使用GetBuffer

总结

以上是内存溢出为你收集整理的c# – 将memorystream对象序列化为字符串全部内容,希望文章能够帮你解决c# – 将memorystream对象序列化为字符串所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236230.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存