XMLTextWriter到XMLDocument

时间:2009-10-29 12:00:17

标签: xml vb.net xmldocument xmltextwriter

我有一个XMLTextWriter写入WebRequest的Stream。一切都按预期工作:

    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

如何拦截正在编写为XMLDocument类型的XML?我尝试从流中抓取它,但这给了我一个'流不支持阅读。'例外(这并不让我感到惊讶)。

由于

1 个答案:

答案 0 :(得分:1)

我认为你不能拦截流,因为它的定义是:

  

一个提供快速,非缓存,仅向前生成流的方式的编写器

非缓存仅向前是您的问题。

那么......有什么阻止你以相反的顺序做这件事吗?

将您的XML写入XmlDocument,并在完成后调用XmlDocument.WriteTo将XML输出到XmlWriter(在这种情况下,您的XmlTextWriter输出到您的请求流)。