我从NHibernate 3 Cookbook(一本优秀的书,顺便说一句)中得到了这段代码,虽然我找不到任何关于它的勘误。我收到错误“无法访问已关闭的流”:
var settings = new XmlWriterSettings { Indent = true };
var serializer = new XmlSerializer(typeof(HbmMapping)); // todo: probably should abstract this out, at least optionally
using (var memStream = new MemoryStream(2048))
using (var xmlWriter = XmlWriter.Create(memStream, settings))
{
serializer.Serialize(xmlWriter, hbmMapping);
memStream.Flush();
memStream.Position = 0;
using (var sr = new StreamReader(memStream))
{
return sr.ReadToEnd();
}
}
在sr.ReadToEnd()行上抛出错误。
答案 0 :(得分:0)
毕竟发现了一个类似的问题Why disposing StreamReader makes a stream unreadable?。基本上,我删除了读者周围的使用,一切都很好。