У меня есть HTTP-обработчик, который привязывает экземпляр XmlTextWriter
к Response.Output
, например, так ...
Sub GenerateXml(ByRef Response As HttpWebResponse)
Using Writer As New XmlTextWriter(Response.Output)
' Build XML
End Using
End Sub
И теперь я также хочу, чтобы тот же XML-файл был сохранен на локальном жестком диске (в дополнение к потоку ответа). Как я могу это сделать?
Вещи, которые я пробовал
1) Очевидное копирование Response.OutputStream в новый 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
Я получаю сообщения об ошибках типа «Метод не поддерживается» и «NullReference»
2) Использование потока памяти и его копирование ...
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
Я получаю сообщения об ошибках типа «Не удается получить доступ к закрытому потоку».
Почему что-то такое простое, как PITA?!