无法访问关闭的流 [英] Cannot access a closed Stream

查看:87
本文介绍了无法访问关闭的流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Friends
我在MemoryStream中制作了一个XmlTextWriter

然后我要使用MemoryStream制作XmlDocument.
但是我收到了以下错误消息:无法访问关闭的流"

Hi Friends
I Made a XmlTextWriter in MemoryStream

Then I Want Make a XmlDocument Using MemoryStream.
But I Have Following a Error Message: "Cannot access a closed Stream"

XmlTextWriter xmlwr = new XmlTextWriter(MemoryStream,System.Text.Encoding.UTF8);
//Other Codes..
xmlwr.Close();
XmlDocument x = new XmlDocument();
x.Load(MemoryStream);

推荐答案

我认为关闭XmlTextWriter也会关闭它正在使用的MemoryStream.

另外,您的MemoryStream在哪里声明? XmlTextWriter在使用前是否已关闭?

也许像这样的东西会更好...

I think that closing the XmlTextWriter will also close the MemoryStream that it is using.

Also, where is your MemoryStream declared? is it closed before you use it it XmlTextWriter?

maybe something like this will work better...

using(MemoryStream ms = new MemoryStream())
{
   using(XmlTextWriter xmlwr = new XmlTextWriter(ms,System.Text.Encoding.UTF8))
   {
      //Other codes...
      ms.Position = 0;//need to set the stream back to the start so it can be read
      XmlDocument x = new XmlDocument();
      x.Load(ms);
   }
}


首先尝试从MemoryStream加载文档,然后关闭XmlTextWriter

这样做:

First try to load the document from MemoryStream then close XmlTextWriter

Do it like this:

XmlDocument X=new XmlDocument()
x.Load(MemoryStream) 
XmlTextWriter .Close()


哇!您用自己的双手关闭流,并想知道为什么将其关闭!
您可以通过关闭xmlwr来关闭它.

您需要不同的方法.所有问题是您的其他代码".
与其直接将某些内容写入某些真正不需要的流中,不如直接写入XmlDocument.将其创建为空并填充.此类表示DOM.每个节点都是可编辑的;您只需添加节点和属性.如果您的其他代码"部分能够编写XML,您将拥有一切.

—SA
Wow! You close the stream with your own hands and wonder why it is closed!
You close it through closing of the xmlwr.

You need different approach. All problem is your "Other codes".
Instead of writing something into some really unwanted stream, write directly to XmlDocument; create it empty and populate. This class represent DOM; and every node is editable; you just add nodes and properties. You have everything if your "Other codes" part is able to write XML.

—SA


这篇关于无法访问关闭的流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆