Click here to Skip to main content
15,745,973 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
See more:
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..
XmlDocument x = new XmlDocument();
Updated 21-Apr-11 1:32am
FatimaAsif 25-Jul-12 6:46am    
i also have this error , i never close stream before but it gives this error ? why?

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();
Share this answer
Sergey Alexandrovich Kryukov 21-Apr-11 5:11am    
Not sure... please check up this: "other codes" probably write the stream and leave the stream position at the end. You need to rewind the memory stream back to zero position before you can load.

Also, see my answer which is an alternative to yours. Why stream?
zsh64 21-Apr-11 5:41am    
Several times this file is made for the same should be stored in MemoryStream
Sergey Alexandrovich Kryukov 21-Apr-11 16:11pm    
Who say it "should be"? Why? If the same file is created N times, N-1 timer are not needed :-)
musefan 21-Apr-11 7:27am    
Yes, you are right! I had forgot about that. Thanks I will amend my solution
musefan 21-Apr-11 7:34am    
Oh, and I used stream because OP using stream. Also, perhaps the 'other codes' creates the XML from a string (which you do need XMLTextWriter for)
First try to load the document from MemoryStream then close XmlTextWriter

Do it like this:

XmlDocument X=new XmlDocument()
XmlTextWriter .Close()
Share this answer
zsh64 21-Apr-11 5:35am    
If i do it Gives the following message
"Root element is missing"
Ankit Rajput 21-Apr-11 7:34am    
In that case, Check your XML data which you have created.
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.

Share this answer
zsh64 21-Apr-11 5:49am    
MemoryStream is used only once
Other code to create XmlTextWriter node
Sergey Alexandrovich Kryukov 21-Apr-11 16:11pm    
No need to create even once! Why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900