I have an HTTP Handler that is binding an XmlTextWriter
instance to Response.Output
like so...
Sub GenerateXml(ByRef Response As HttpWebResponse)
Using Writer As New XmlTextWriter(Response.Output)
' Build XML
End Using
End Sub
And now I also want the same XML to be saved to the local hard drive (in addition to being streamed to the response). How can I do it?
Things I have tried
1) The obvious, copying the Response.OutputStream to a new FileStream...
Sub GenerateXml(ByRef Response As HttpWebResponse) Using Writer As New XmlTextWriter(Response.Output) Build XML Here End Using // Also tried this inside the Using, with and without .Flush() CopyStream(Response.OutputStream, File.Create("C:\test.xml")) End Sub Sub CopyStream(ByRef Input As Stream, ByRef Output As Stream) Dim Buffer(1024) As Byte Dim Read As Integer Using Input Using Output While (Read = Input.Read(Buffer, 0, Buffer.Length)) > 0 Output.Write(Buffer, 0, Read) End While End Using End Using End Sub
I get errors like "Method not supported" and "NullReference"
2) Using a Memory Stream and then copying it...
Using Memory As New MemoryStream()
Using Writer As New XmlTextWriter(Memory, Encoding.UTF8)
Build XML Here
End Using
// Tried outside of the Using, with and without Flush()
CopyStream(Memory, Response.OutputStream)
CopyStream(Memory, File.Create("C:\test.xml"))
End Using
I get errors like "Cannot access a closed Stream."
Why is something so simple such a PITA?!