Link to home
Start Free TrialLog in
Avatar of PNRT
PNRT

asked on

VB.Net, send and receive xml to web service

Hi Experts.   I need to send an xml file to a web service and receive an xml file back in return.  Could someone give me an example of how to achieve this in VB please?   I'm struggling to find simple code examples.  Thanks in advance
Avatar of Rose Babu
Rose Babu
Flag of India image

Hi,

Try the below code which will send and receive an XML content in the web service.

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://www.domain.com/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
    Inherits System.Web.Services.WebService

    Class InputXmlFileRequest
        Public xmlInputData As String
    End Class

    Class OutputXmlFileResponse
        Public xmlOutputData As String
    End Class

    <WebMethod()> _
    Public Function SendXmlFile(ByVal xmlInput As InputXmlFileRequest) As OutputXmlFileResponse

        Dim objXML As New XmlDocument
        objXML.LoadXml(xmlInput.xmlInputData)
        ' Do your XML process if any

        ' format your Output XML
        Dim outXML As String = String.Empty
        outXML &= "<?xml version='1.0' encoding='utf-8'?> "
        outXML &= "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>"
        outXML &= "  <soap:Body>"
        outXML &= "    <CelsiusToFahrenheitResponse xmlns='http://tempuri.org/'>"
        outXML &= "      <CelsiusToFahrenheitResult>Test Output " & Today.ToString("dd/MM/yyyy") & " " & Now.ToString("HH:mm:ss") & "</CelsiusToFahrenheitResult>"
        outXML &= "    </CelsiusToFahrenheitResponse>"
        outXML &= "  </soap:Body>"
        outXML &= "</soap:Envelope>"

        Dim resp As New OutputXmlFileResponse
        resp.xmlOutputData = outXML

        Return resp

    End Function

End Class

Open in new window

Request :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:dom="http://www.domain.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <dom:SendXmlFile>         
         <dom:xmlInput>            
            <dom:xmlInputData><![CDATA[<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <CelsiusToFahrenheit xmlns="http://tempuri.org/">
      <Celsius>string</Celsius>
    </CelsiusToFahrenheit>
  </soap:Body>
</soap:Envelope>]]></dom:xmlInputData>
         </dom:xmlInput>
      </dom:SendXmlFile>
   </soapenv:Body>
</soapenv:Envelope>

Open in new window

Response :

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <SendXmlFileResponse xmlns="http://www.domain.com/">
         <SendXmlFileResult>
            <xmlOutputData><![CDATA[<?xml version='1.0' encoding='utf-8'?> <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>  <soap:Body>    <CelsiusToFahrenheitResponse xmlns='http://tempuri.org/'>      <CelsiusToFahrenheitResult>Test Output 26/08/2013 13:40:01</CelsiusToFahrenheitResult>    </CelsiusToFahrenheitResponse>  </soap:Body></soap:Envelope>]]></xmlOutputData>
         </SendXmlFileResult>
      </SendXmlFileResponse>
   </soap:Body>
</soap:Envelope>

Open in new window

Try this once. Hope this is what you are expecting.
Avatar of PNRT
PNRT

ASKER

Many Thanks srosebabu.  This looks exactly what I'm looking for.   Please can you advise how I would call the function from my code though?   Many Thanks again for the help
ASKER CERTIFIED SOLUTION
Avatar of Rose Babu
Rose Babu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What template will I open for the code? windows console or asp.net?