在.Net Web服务中的正文SOAP消息中按顺序读取xml元素

时间:2018-08-08 11:02:32

标签: vb.net web-services soap

如果我收到这两种不同的SOAP消息。

消息1:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:orve="http://temp.uri">
   <soapenv:Header/>
   <soapenv:Body>
      <orve:operation>
         <foo>foo</foo>
         <bar>foo</bar>
      </orve:operation>
   </soapenv:Body>
</soapenv:Envelope>

消息2:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:orve="http://temp.uri">
   <soapenv:Header/>
   <soapenv:Body>
      <orve:operation>
         <value0>hello</value0>
         <value1>world!</value1>
      </orve:operation>
   </soapenv:Body>
</soapenv:Envelope>

我想从消息1中读取值 foo bar 或从消息2中读取 value0 value1 当我收到SOAP消息时。那可能吗?我尝试使用XmlArray参数,但无法正常工作

    <WebMethod(Description:="operation")> _
    <System.Web.Services.Protocols.SoapRpcMethodAttribute("http://mywebsite.com/mywebservice.asmx?method=operation", RequestNamespace:="", ResponseNamespace:="http://response.uri", Use:=System.Web.Services.Description.SoapBindingUse.Literal)> _
    Public Function operation(<XmlArray> ByVal extraInfo As XmlElement()) As <System.Xml.Serialization.XmlElementAttribute("operation")> WSResponse
        'extraInfo is Nothing :(
    End Function    

编辑1

我找到了类似的解决方案

    <WebMethod(Description:="operation")> _
    <System.Web.Services.Protocols.SoapRpcMethodAttribute("http://mywebsite.com/mywebservice.asmx?method=operation", RequestNamespace:="", ResponseNamespace:="http://response.uri", Use:=System.Web.Services.Description.SoapBindingUse.Literal)> _
    Public Function operation() As <System.Xml.Serialization.XmlElementAttribute("operationReturn")> RespuestaWS

        Dim xmlSoapRequest As XmlDocument = New XmlDocument()

        Using receiveStream As Stream = HttpContext.Current.Request.InputStream
            receiveStream.Position = 0

            Using readStream As StreamReader = New StreamReader(receiveStream, Encoding.UTF8)
                xmlSoapRequest.Load(readStream)
            End Using
        End Using

        Dim fooOrValue0 = xmlSoapRequest.GetElementsByTagName("foo")
        If fooOrValue0 .Count = 0 Then
            fooOrValue0 = xmlSoapRequest.GetElementsByTagName("value0")
        End If

        'And then use fooOrValue0.InnerText

    End Function

但是我想要的是独立于其名称和使用方法参数来获取值。

0 个答案:

没有答案