C # using + XmlWriter.Create = "Unable to access private stream."

Why does it work:

using (var ms = new MemoryStream()) { using (var dummy = new StreamWriter(ms)) { var sw = new StreamWriter(ms); sw.WriteLine("Hello World"); sw.Flush(); using (StreamReader rdr = new StreamReader(ms)) { ms.Position = 0; textBoxExc.Text = rdr.ReadToEnd(); } } } 

but this does not work ("Unable to access the private stream"): Only the difference is var dummy = XmlWriter.Create(ms) instead of var dummy = new StreamWriter(ms)

 using (var ms = new MemoryStream()) { using (var dummy = XmlWriter.Create(ms)) { var sw = new StreamWriter(ms); sw.WriteLine("Hello World"); sw.Flush(); using (StreamReader rdr = new StreamReader(ms)) { ms.Position = 0; textBoxExc.Text = rdr.ReadToEnd(); } } } 

Stack trace:

 System.ObjectDisposedException was unhandled by user code Message=Cannot access a closed Stream. ObjectName="" StackTrace: w System.IO.__Error.StreamIsClosed() w System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) w System.Xml.XmlUtf8RawTextWriter.FlushBuffer() w System.Xml.XmlUtf8RawTextWriter.Flush() w System.Xml.XmlWellFormedWriter.Close() w System.Xml.XmlWriter.Dispose(Boolean disposing) w System.Xml.XmlWriter.Dispose() w SerializeTest.MainPage.buttonExc_Click(Object sender, RoutedEventArgs e) w System.Windows.Controls.Primitives.ButtonBase.OnClick() w System.Windows.Controls.Button.OnClick() w System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) w System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) w MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) InnerException: 

Also this does not work (same error):

 using (var ms = new MemoryStream()) { using (var writer = XmlWriter.Create(ms)) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(writer, objectToSave); writer.Flush(); ms.Position = 0; using (StreamReader rdr = new StreamReader(ms)) { return rdr.ReadToEnd(); } } } 

Stack trace:

 System.ObjectDisposedException was unhandled by user code Message=Cannot access a closed Stream. ObjectName="" StackTrace: w System.IO.__Error.StreamIsClosed() w System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) w System.Xml.XmlUtf8RawTextWriter.FlushBuffer() w System.Xml.XmlUtf8RawTextWriter.Flush() w System.Xml.XmlWellFormedWriter.Close() w System.Xml.XmlWriter.Dispose(Boolean disposing) w System.Xml.XmlWriter.Dispose() w SerializeTest.SerializeToStringTest[T](T objectToSave) w SerializeTest.MainPage.button2A_Click(Object sender, RoutedEventArgs e) w System.Windows.Controls.Primitives.ButtonBase.OnClick() w System.Windows.Controls.Button.OnClick() w System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e) w System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e) w MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName) InnerException: 

but this works (only the difference is StreamReader w / o using ):

 using (var ms = new MemoryStream()) { using (var writer = XmlWriter.Create(ms)) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(writer, objectToSave); writer.Flush(); ms.Position = 0; StreamReader rdr = new StreamReader(ms); return rdr.ReadToEnd(); } } 
+7
c # using silverlight xmlwriter
Mar 30 2018-11-11T00:
source share
2 answers

Can you give us a full trace of the exception stack? My first guess is that XmlWriter is still trying to access the stream in the Dispose() method of XmlWriter .

In your second and fourth code examples, you put the StreamReader in a block block. This calls the Dispose() StreamReader at the end of this block. This method closes both the reader and the underlying thread. After that, the Dispose() method of XmlWriter will no longer be able to access the stream.

Update: Based on stackstrace, it seems like I'm right. The Dispose() method calls Close() , which in turn wants to flush an already closed stream. This seems like a mistake, since there was nothing to hide.

You have already given a solution: do not close memystream before placing XmlWriter.

(I assume that you know that the used block implicitly controls the used object, and that placing a StreamReader or StreamWriter implicitly controls (and closes) the underlying stream.)

+4
Mar 30 2018-11-11T00:
source share

just move the reading from the MemoryStream block at the same level as writing to it.

 using (var ms = new MemoryStream()) { using (var writer = XmlWriter.Create(ms)) { var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(writer, objectToSave); writer.Flush(); ms.Position = 0; } using (StreamReader rdr = new StreamReader(ms)) { return rdr.ReadToEnd(); } } 
+13
Jan 12 2018-12-12T00:
source share



All Articles