Home » Technology » C# » A C# Example For Querying Data From A SOAP Web Service

A C# Example For Querying Data From A SOAP Web Service

Continuing on my last article, which showed a simple SOAP request in PHP, this article will do the same using C#. The IDE I am using is Visual Studio Community Edition. The core technologies here are C#, and SOAP. I presume a familiarity with C#. SOAP, or Simple Object Access Protocol, is a Web Service which utilizes XML as it’s format and, in our examples, the data is transmitted via HTTP. As such, I presume a basic understanding of HTTP, and general Markup as you might find in HTML or XML.

The WSDL file

Our query against the SOAP web service will be dictated by the WSDL file provided for the web service in question. The WSDL file provides a contract, or user manual, listing the available functions we can call, along with information such as input data we can provide, and data we can expect to be returned.

I will be using a publicly available SOAP Web Service, and as such, it’s WSDL file; the file can be viewed in your browser by following the link, or downloaded and opened for analysis using Notepad, or Notepad++ in a Microsoft Windows environment:

http://footballpool.dataaccess.eu/data/info.wso?wsdl

Reviewing the WSDL file, we can see a number of functions; here is a simple one with one input variable:

<xs:element name=”AllMidFields>
<xs:complexType>
<xs:sequence>
<xs:element name=”sCountryName type=”xs:string/>
</xs:sequence>
</xs:complexType>
</xs:element>
The parent tag lists the function name: AllMidFields; and the input is a String, sCountryName. It is that simple; there is a function AllMidFields that takes a single input, the Country Name (sCountryName).
In Visual Studio, create a Console Application: File > New > Project > Console Application. Now, under Project, “Add Service Reference”, you will add the WSDL file as a reference; what actually happens is that the file is parsed and supporting classes are created.
C# Service Reference WSDL
Moving on. Having added the Service Reference, which in this case is called SeriviceReference1 (see above), we can put together the code to process the function AllMidFields and output the data. unlike the PHP example, we need to explicitly state the endpoint; there are two endpoints listed in the actually WSDL file:
<service name=”Info>
<documentation>
This Visual DataFlex Web Service exposes functions for the current football pool
</documentation>
<port name=”InfoSoap binding=”tns:InfoSoapBinding>
<soap:address location=”http://footballpool.dataaccess.eu/data/info.wso/>
</port>
<port name=”InfoSoap12 binding=”tns:InfoSoapBinding12>
<soap12:address location=”http://footballpool.dataaccess.eu/data/info.wso/>
</port>
</service>
Trial and error showed that InfoSoap12 is the proper endpoint. And the code is:

using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string country = “Australia”;
string endpointConfigurationName = “InfoSoap12”;
InfoSoapTypeClient iSTC = new InfoSoapTypeClient(endpointConfigurationName);
ArrayOfString aOS = iSTC.AllMidFields(country);
if (!aOS.Equals(null))
{
System.Console.WriteLine(“There are {0} returned values.”, aOS.Count);

foreach (string element in aOS)
{
System.Console.WriteLine(element);
}
} else
{
System.Console.WriteLine(“{0}”, “No data returned.”);
}
System.Threading.Thread.Sleep(100000);
}
}
}

And these are the results when run:

      There are 11 returned values.
      Ben Halloran
      Dario Vidosic
      James Holland
      James Troisi
      Mark Bresciano
      Mark Milligan
      Massimo Luongo
      Matt McKay
      Mile Jedinak
      Oliver Bozanic
      Tommy Oar

 

All content is © copyrighted by the owner of this website, 2014 and beyond | Check us out on Twitter