簡體   English   中英

在vb.net中更改SOAP請求的xml文件節點值

[英]Change xml file node value for SOAP request in vb.net

我需要將XML文件(SOAP + xml)發送到Web服務,但是我需要在XML文件中更改2節點值。 這是XML文件:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns2826:get_order_data xmlns:ns2826="http://tempuri.org">
<periode>
<tgl1 xsi:type="xsd:string">Date 1</tgl1>
<tgl2 xsi:type="xsd:string">Date 2</tgl2>
</periode>
</ns2826:get_order_data>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我需要將日期1和日期2更改為datetime值。 到目前為止,我已經嘗試過更改xml文件,如下所示:

    Sub requestByDate()
        'edit file xml sebelum request'
        Dim myXmlDocument As XmlDocument = New XmlDocument()
        myXmlDocument.Load("C:\xmlRequest\requestOrderdata.xml")

        Dim node As XmlNode
        node = myXmlDocument.DocumentElement

        Dim node2 As XmlNode 'Used for internal loop.'

        For Each node In node.ChildNodes
            For Each node2 In node.ChildNodes
                'MsgBox(node2.InnerText)'
                If node2.Name = "ns2826:get_order_data" Then
                   Dim newkey As String

                    newkey = "<" & "periode" & ">" & vbCrLf
                    newkey = newkey & "<" & "tgl1 xsi:type=" & ControlChars.Quote & "xsd:string" & ControlChars.Quote & ">10/06/2015 01:00:00</tgl1>" & vbCrLf
                    newkey = newkey & "<tgl2 xsi:type=" & ControlChars.Quote & "xsd:string" & ControlChars.Quote & ">10/06/2015 03:00:00</tgl2>" & vbCrLf
                    newkey = newkey & "</periode>"

                    MsgBox("Old Key = " & node2.InnerText & Strings.Chr(9) & "New key = " & newkey)
                    node2.InnerText = newkey
                    myXmlDocument.Save("C:\xmlRequest\requestOrderData2.xml")


                End If
                Next
            Next
            'selesai edit'
    End Sub

但是它不起作用,因為新的xml文件不是有效的XML請求文件(如果我以該新的xml文件作為請求運行程序,則它將返回“存在多個根元素”)。 還有其他方法可以更改日期1和日期2的值嗎?

查看較新的API XDocument來代替您的XmlDocument方法,例如:

Dim doc = XDocument.Load("C:\xmlRequest\requestOrderdata.xml")
Dim ns2826 As XNamespace = "http://tempuri.org"

'find and update <tgl1>'
Dim tgl1 = doc.Descendants(ns2826 + "get_order_data").Elements("periode").Elements("tgl1").First
tgl1.Value = "10/06/2015 01:00:00"

'find and update <tgl2>'
Dim tgl2 = doc.Descendants(ns2826 + "get_order_data").Elements("periode").Elements("tgl2").First
tgl2.Value = "10/06/2015 03:00:00"

doc.Save("C:\xmlRequest\requestOrderData2.xml")

您還可以使用VB中可用的XML軸語法,例如:

Imports <xmlns:ns2826="http://tempuri.org">

.....

'find <tgl1>'
Dim tgl1 = doc...<ns2826:get_order_data>.<periode>.<tgl1>.First

'find <tgl2>'
Dim tgl2 = doc...<ns2826:get_order_data>.<periode>.<tgl2>.First

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM