Friday, October 31, 2008

Consuming Webservice using MSSoap

Even though majority of the programmers have started using, there may be cases where you have a legacy system that still needs to consume webservice. This is how to do it with MSSoap.

First thing to be aware of is you may encounter issue if the webservice returns XML, so have it return a string instead. This is easily done by using a wrapper function. For example, if you have a webmethod say getLotData, just create a getLotDataStr as follows:

<WebMethod()> _
Public Function getLotData(ByVal pLotID As String) As XmlDocument
End Function


Public Function getLotDataStr(ByVal pLotID As String) As String
Dim tmpStr As String = ""
Dim xDoc As XmlDocument

xDoc = getLotData(pLotID)
tmpStr = xDoc.OuterXml

Return tmpStr
End Function

To consume the webservice, in vbscript or VB, you then use the MSSoap client as follows:

Dim wsClient As New MSSOAPLib30.SoapClient30
Dim urlStr as String = ""
Dim sReply, lotID As String
Dim xDoc As DOMDocument30

lotID = ...
wsClient.MSSoapInit urlStr
sReply = wsClient.getLotDataStr(lotID)

xDoc.loadXML sReply


Once the information is loaded to the XmlDocument, you can then access the information by using the getElementsByTagName. You can also access the inner text by access the Text Property.


Dim lotNode As IXMLDOMNode
Dim processStep As String

Set lotNode = xDoc.getElementByTagName("processID")
ProcessStep = lotNode.Text

'If it is an attribute then access it by the Attributes as in
ProcessStep = lotNode.Attributes.getNameItem("Name").Text

You can also use the DomDocument30 data type to create the parameters if the Webservice requires a XML as a parameter.

No comments: