![](/img/trans.png)
[英]Root Element Missing - Creating Xmldocument using XmlTextWriter
[英]XMLTextWriter to XMLDocument
我有一個XMLTextWriter寫入WebRequest的流。 一切正常,應該:
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? 我嘗試從流中抓取它,但這給了我一個“流不支持閱讀”的提示。 例外(這並不令我感到驚訝)。
謝謝
我認為您無法攔截流,因為按照它的定義,它是:
提供快速,非緩存,僅前向方式生成流的編寫器
非緩存和僅轉發是您遇到的問題。
那么...有什么阻止您以相反的順序進行操作的?
將XML寫入XmlDocument,完成后調用XmlDocument.WriteTo將XML輸出到XmlWriter(在這種情況下,您的XmlTextWriter輸出到請求流)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.