在vb.net中更改SOAP请求的xml文件节点值

在vb.net中更改SOAP请求的xml文件节点值,xml,vb.net,soap,Xml,Vb.net,Soap,我需要将XML文件(SOAP+XML)发送到webservice,但我需要更改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:SO

我需要将XML文件(SOAP+XML)发送到webservice,但我需要更改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
我需要将日期1和日期2更改为日期时间值。到目前为止,我已尝试如下更改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
Sub-requestByDate()
'编辑文件xml sebelum请求'
Dim myXmlDocument As XmlDocument=新的XmlDocument()
myXmlDocument.Load(“C:\xmlRequest\requestOrderdata.xml”)
Dim节点作为XmlNode
node=myXmlDocument.DocumentElement
Dim node2作为XmlNode“用于内部循环”
对于node.ChildNodes中的每个节点
对于node.ChildNodes中的每个node2
'MsgBox(node2.InnerText)'
如果node2.Name=“ns2826:获取订单数据”,则
将newkey设置为字符串
newkey=”“&vbCrLf
newkey=newkey&“10/06/2015 01:00:00”&vbCrLf
newkey=newkey&“10/06/2015 03:00:00”&vbCrLf
newkey=newkey&“”
MsgBox(“Old Key=“&node2.InnerText&Strings.Chr(9)”和“New Key=“&newkey”)
node2.InnerText=newkey
保存(“C:\xmlRequest\requestOrderData2.xml”)
如果结束
下一个
下一个
“selesai编辑”
端接头
但是它不起作用,因为新的xml文件不是有效的xml请求文件(如果我以这个新的xml文件作为请求运行程序,它将返回“有多个根元素”)。 是否有其他方法可以更改日期1和日期2的值?

查看更新的API以替换您的
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")
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
Dim doc=XDocument.Load(“C:\xmlRequest\requestOrderdata.xml”)
尺寸ns2826为XNamespace=”http://tempuri.org"
“查找并更新”
Dim tgl1=文档子体(ns2826+“获取订单数据”)。元素(“周期”)。元素(“tgl1”)。首先
tgl1.Value=“10/06/2015 01:00:00”
“查找并更新”
Dim tgl2=文档子体(ns2826+“获取订单数据”)。元素(“周期”)。元素(“tgl2”)。首先
tgl2.Value=“10/06/2015 03:00:00”
doc.Save(“C:\xmlRequest\requestOrderData2.xml”)
您还可以使用VB中可用的语法,例如:

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")
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
导入
.....
“找到”
尺寸tgl1=文件……首先
“找到”
Dim tgl2=文件……首先

您使用的是什么版本的.NET framework?然后
XDocument
就可以为您提供了。参见我的答案,例如如何使用
XDocument
感谢您的友好回答@har07,使用您的示例,我在第一个(“tgl1”)和第一个(“tgl2”)上得到了2个错误。错误消息是:类型为“String”的错误1值无法转换为“System.Func(of System.Xml.Linq.XElement,Boolean)”您知道可能是什么问题吗?现在可以工作了,谢谢您的回答@har07