WebService in WinApp c#

Associate
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
We have a webservice that I can call from a webpage via an ajax call

Code:
    $.ajax({
                type: "GET",
                url: "http://server/GetPostcodes.svc/xml/" + ID,
                contentType: "text/xml; charset=utf-8",
                async: true,

                success: function (data) {
                        var parser = new DOMParser();
                        var xmlDoc = parser.parseFromString(data.childNodes[0].textContent,"text/xml");
                        var xmlResultCount = xmlDoc.childNodes[0].childElementCount;
                        
                        var columnHeadArray = ["Street 1", "Street 2", "Street 3", "City", "Country", "Postcode"];

                        bindData(tableID, columnHeadArray);
                        ....

It returns an address from a postcode or partial postcode given to it.

We want to reuse this service in a windows forms app - how can I call this service and parse the results via the winapp?


Matt
 
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
Ok got:

Code:
WebRequest webRequest = WebRequest.Create("http://server/GetPostcodes.svc/xml/DN12");
WebResponse webResp = webRequest.GetResponse();

Stream dstream = webResp.GetResponseStream();

It's just parsing the resulting stream into a datatable?

Matt
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
You could read the stream line-by-line and build the results into a DataTable yourself:

Stream dstream = webResp.GetResponseStream();
StreamReader reader = new StreamReader( dstream );
While(!reader.EndOfStream()){
string lineText = reader.ReadLine();

//do something with this line of text

}
 
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
Thanks for the reply.

I can now iterate over the text returned but googling suggests I can use desrialise to do what I am trying to do, however my returned xml looks like this:

Code:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfPostcodes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Postcodes>
    <PostCode>DN16 8BD</PostCode>
    <Street1>1 Kings Grove </Street1>
    <Street2>Denaby</Street2>
    <Street3 />
    <City>Doncaster</City>
    <Country>United Kingdon</Country>
  </Postcodes>

Note the <Arrayofpostcodes, is this ill formed should there be a root or???

Am new to this so struggling my way through it at the moment.


Matt
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
If you're struggling, the simplest way is just to keep each line as a string and parse it manually. It's probably not the most efficient way of doing it but if you're just looking up a single postcode at a time then it should be fine.

So, expanding on the code I posted above:

Stream dstream = webResp.GetResponseStream();
StreamReader reader = new StreamReader( dstream );

string postcode = "";
string street1 = "";
string street2 = "";

While(!reader.EndOfStream()){
string lineText = reader.ReadLine();

if(lineText.IndexOf('<postcode>') > 0)
{
//found the line like "<PostCode>DN16 8BD</PostCode>"
$postcode = lineText.substring(10, lineText.Length-21);
//start substring at position 10 (after the "<PostCode>" part)
//length of the actual postcode is the length of the line minus the length of the "<PostCode>" and "</PostCode>" strings (eg lineText.Length-21)
}

if(lineText.IndexOf('<street1>') > 0)
{
$street1 = lineText.substring(9, lineText.Length-19);
}
if(lineText.IndexOf('<street2>') > 0)
{
$street2 = lineText.substring(9, lineText.Length-19);
}

//repeat for all lines
}

I always get the numbers wrong when doing start indexes on substrings and things like that so you'll probably need to adjust by plus/minus 1 on some of them.
 
Associate
Joined
31 Jan 2018
Posts
539
Location
Bury St Edmunds
There are a couple of options you could look at.

You could also look at adding a service reference to your application by adding http://server/GetPostcodes.svc for the address and then choose a namespace for the service for example "Application25ServiceReference", then you can just create a web service client and then call the methods that are available and then you should then be able to access the properties with ease.

Code:
Application25ServiceReference.ApplicationClient applicationClient = new Application25ServiceReference.ApplicationClient();

var results = applicationClient.GetPostcodes("DN12);


Alternatively, you would get the XML result from the Webservice as a string and then use I would then use the XmlSerializer I use a lot. To do this you would need to create a class called PostCodes with the same parameters as the XML e.g PostCode, Street1 ...

And then using it would do something like this:

Code:
string xmlString ="Postcode data from web service";
StringReader stringReader = new StringReader(xmlString);
XmlSerializer serializer = new XmlSerializer(<List<PostCodes>>);
var postcodes =(List<PostCodes>)serializer.Deserialize(stringReader);
 
Last edited:
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
chroniclard,

That example was superb, thanks.

So as the first three lines of the xml are:
Code:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfPostcodes xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Postcodes>


With the last line being
Code:
</Postcodes>
</ArrayOfPostcodes>


I assume I would create a class for ArrayofPostcodes, but what about the first line?

Matt
 
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
Shoeyuk,

.ApplicationClient I have no option for that only my methods from the service - .GetPostcodesClient, .IGetPostcodes, .IGetPostcodesChannel. have you added the ApllicationClien?

Matt
 

Pho

Pho

Soldato
Joined
18 Oct 2002
Posts
9,324
Location
Derbyshire
Honestly, just let Visual Studio consume your WCF service for you - it's what it was designed for, it will create all the correct classes and mappings automatically :p
 
Associate
Joined
31 Jan 2018
Posts
539
Location
Bury St Edmunds
Shoeyuk,

.ApplicationClient I have no option for that only my methods from the service - .GetPostcodesClient, .IGetPostcodes, .IGetPostcodesChannel. have you added the ApllicationClien?

Matt

Create a GetPostcodesClient object and then there will be a method within the client to get the postcodes.

Without know the web service you will have to have a look at the objects that visual studio creates for you.
 
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
Shoeyuk,

I've created:

GetPostcodesServiceReference1.GetPostcodesClient getPostcodesClient = new GetPostcodesServiceReference1.GetPostcodesClient();

but it errors with "System.InvalidOperationException: 'Could not find default endpoint element that references contract 'GetPostcodesServiceReference1.IGetPostcodes' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.'"

Googling this I appear to be missing config in my app.config file, if so what do I add in here - getting way beyond my knowledge here now.

Matt
 
Associate
OP
Joined
27 Jan 2005
Posts
1,311
Location
S. Yorks
Ah added the link in app.config but getting a

'The communication object, System.ServiceModel.ChannelFactory`1[GetPostcodesServiceReference1.IGetPostcodes], cannot be used for communication because it is in the Faulted state.'

???


Matt
 
Back
Top Bottom