【问题标题】:Change xml file node value for SOAP request in vb.net在 vb.net 中更改 SOAP 请求的 xml 文件节点值
【发布时间】:2015-06-12 02:10:39
【问题描述】:

我需要将 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 更改为日期时间值。到目前为止,我已经尝试像这样更改 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 的值?

【问题讨论】:

  • 您使用的是哪个版本的 .NET 框架?
  • 那么您可以使用XDocument。请参阅我的答案,例如如何使用XDocument

标签: xml vb.net soap


【解决方案1】:

查看更新的 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 axis语法,例如:

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

【讨论】:

  • 感谢您的友好回答@har07,使用您的示例我在 First("tgl1") 和 First("tgl2") 上得到 2 个错误。错误消息是:错误 1 ​​'String' 类型的值无法转换为 'System.Func(Of System.Xml.Linq.XElement, Boolean)' 你知道可能是什么问题吗?
  • 它现在可以工作了,感谢您的出色回答@har07
猜你喜欢
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多