Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 533513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:30:58+00:00 2026-05-13T09:30:58+00:00

I have an HTTP Handler that is binding an XmlTextWriter instance to Response.Output like

  • 0

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?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:30:59+00:00Added an answer on May 13, 2026 at 9:30 am

    You can’t wire up Response.OutputStream in that manner. However, you could do one of two things:

    1. Create a method that calls Stream.Write for both your test file and Response.OutputStream.

      using (var writerResponse = new XmlTextWriter(Response.OutputStream))
      using (var writerFile = new XmlTextWriter(File.Create(@"c:\test.xml"))
      {
          StartElement(writerResponse, writerFile, @"test");
      }
      
    2. Create a wrapper class which encapsulates two streams to be written to and provides all of the stream methods you require (Write etc).

      public class DualStream : Stream
      {
          public DualStream(Stream first, Stream second)
          {
          }
      
          public override bool CanRead { get { return false; } }
          public override bool CanWrite { get { return true; } }
      
          public override void Write(byte[] buffer, int offset, int count)
          {
              this.first.Write(buffer, offset, count);
              this.second.Write(buffer, offset, count);
          }
      }
      
      // ...
      
      using (var writerFile = new XmlTextWriter(File.Create(@"c:\test.xml")))
      using (var writer = new XmlTextWriter(
            new DualStream(Response.OutputStream, writerFile)), null))
      {
           // use writer
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 287k
  • Answers 287k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why are you calling ToList()? RIA returns an EntitySet which… May 13, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer Thanks, the join wasn't quite as simple as the example… May 13, 2026 at 5:10 pm
  • Editorial Team
    Editorial Team added an answer Yes. The first time foo() is called, Bar bar will… May 13, 2026 at 5:10 pm

Related Questions

I have the following content in a Window ( removed unnecessary sections ): XAML:
I am exploring the Silverlight attached behaviors mechanism in order to use the Model-View-ViewModel
I'm trying to use the Class methods of prototype.js to manage my object hierarchy
I have the following problem. On my machine I have a SL 3 application,
I am trying to update some object properties by calling UpdateModel(myObject, new[] { "stringprop1",

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.