views:

263

answers:

1

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

+1  A: 

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
Almost exactly what I ended up doing a while back. Better late than never, thanks!
Nick Spiers