2

I've got an XMLTextWriter writing to a Stream of a WebRequest. Everything works as it should:

    Dim wr As WebRequest = WebRequest.Create("https://wwwcie.ups.com/ups.app/xml/ShipAccept")
    With wr
        .Method = "POST"
        .ContentType = "application/x-www-form-urlencoded"
    End With
    Dim requestStream As Stream = wr.GetRequestStream
    Using requestStream

        Dim x As New XmlTextWriter(requestStream, Encoding.UTF8)

        Using x
            With x

                .WriteStartDocument()
                'XML

                .WriteStartElement("ShipmentAcceptRequest")
                'ShipmentAcceptRequest

                .WriteStartElement("Request")
                'Request

                .WriteElementString("RequestAction", sar.Request.RequestAction)

                '/Request
                .WriteEndElement()

                .WriteElementString("ShipmentDigest", sar.ShipmentDigest)

                '/ShipmentAcceptRequest
                .WriteEndElement()

                '/XML
                .WriteEndDocument()

                .Flush()

            End With
        End Using

    End Using

How can I intercept this XML that's being written as an XMLDocument type? I tried snagging it from the stream but that gave me a 'The stream does not support reading.' exception (which didn't surprise me).

Thanks

Nick Spiers
  • 2,344
  • 2
  • 19
  • 31
  • This is still a problem for me, if anyone can help. Do I just need to rewrite the entire XML with another TextWriter? – Nick Spiers Nov 02 '09 at 16:54

1 Answers1

1

I don't think you can intercept the stream, because by it's definition it is:

a writer that provides a fast, non-cached, forward-only way of generating streams

non-cached and forward-only being your problems there.

So... anything stopping you from doing it in reverse order?

Write your XML to an XmlDocument, and when you're finished call XmlDocument.WriteTo to output the XML to an XmlWriter (in this case your XmlTextWriter outputting to your request stream).

Xiaofu
  • 15,523
  • 2
  • 32
  • 45