0

I'm currently in the process of learning .Net using C# and have begun experimenting with Web Services. With the help of someone on another thread: (Define which Endpoint to use when consuming a Web Service) I've managed to sort out my Endpoints and get some form of response form the Web Service I am using. My only problem now is that the response I receive seems to be an array and I need to be able to parse individual nodes from this array.

To shed some light on this, here is the XML structure that would be returned from calling the web service:

<ForecastReturn xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://ws.cdyne.com/WeatherWS/">
  <Success>true</Success>
  <ResponseText>City Found</ResponseText>
  <State>DE</State>
  <City>Newark</City>
  <WeatherStationCity>Dover AFB</WeatherStationCity>
  <ForecastResult>
    <Forecast>
      <Date>2014-04-22T00:00:00</Date>
      <WeatherID>6</WeatherID>
      <Desciption>Showers</Desciption>
      <Temperatures>
        <MorningLow>45</MorningLow>
        <DaytimeHigh>71</DaytimeHigh>
      </Temperatures>
      <ProbabilityOfPrecipiation>
        <Nighttime>00</Nighttime>
        <Daytime>60</Daytime>
      </ProbabilityOfPrecipiation>
    </Forecast>
  </ForecastResult> 
</ForecastReturn>

I want to be able to access the nodes within this response by name or similar. So if I state I want the temperature, it will return the temperature however using result.Temperature doesn't seem to work in this case suggesting that the nodes do not become properties of the response object.

Here is my code so far:

Unhandled Exception: System.InvalidOperationException: An endpoint configuration section for contract 'Service1Reference.WeatherSoap'
could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

My question is, how do I specify that my call to the web service should use one of the SOAP endpoints? My code so far can be found below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication1.Service1Reference;

namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.Write("Enter ZipCode: ");
      var line = Console.ReadLine();
      if (line == null)
      {
        return;
      }

      WeatherSoapClient svc = null;
      bool success = false;
      try
      {
        svc = new WeatherSoapClient();

        var request = line;
        var result = svc.GetCityForecastByZIP(request);

        Console.WriteLine("The result is:");
        Console.WriteLine(result);
        Console.Write("ENTER to continue:");
        Console.ReadLine();

        svc.Close();
        success = true;
      }
      finally
      {
        if (!success && svc != null)
        {
          svc.Abort();
        }
      }
    }
  }
}

How can I adapt this to access the different nodes in my response from this web service?

Any help would be greatly appreciated.

Community
  • 1
  • 1
jezzipin
  • 4,110
  • 14
  • 50
  • 94
  • Did you make sure you updated your service reference after you made your last changes to the return value of GetCityForecastByZIP? Also, when you say "XML structure that would be returned", do you mean that this is the XML structure that *is* returned? Have you checked that with Fiddler? – Markus Apr 24 '14 at 16:46
  • I don't know if I've got it right but you proposed 2 questions here: how to choose the endpoint (this ones seems like you have already solved) and how to find the information inside your response. For the second one, the web service must have defined a "ForecastReturn" class as the response for the GetCityForecastByZIP method. To get the temperatures it should be something like: result.ForecastResult.Forecast.Temperatures, is that what you need? You don't need to parse the XML, XML is just the transport chosen for this web services, some others such as Json might be used here. – Jportelas Apr 24 '14 at 17:11
  • This isn't the case though. I've tried using result.ForecastResult.Forecast.Temperatures but with this the project won't even compile in Visual Studio. I get System.Array does not contain a definition for Forecast. – jezzipin Apr 25 '14 at 09:02

1 Answers1

0

In this instance, some of the nodes are returned as part of an array. The array is referenced using the parent container. Therefore in my example above I needed to access the nodes by name:

int count = 0;
    int total = result.ForecastResult.Length;
    foreach (var rs in result.ForecastResult){
      if (count > 0)
      {
        Console.WriteLine("************************");
      }
      Console.WriteLine("Date:" + rs.Date);
      Console.WriteLine("Forecast:" + rs.Desciption);
      Console.WriteLine("Temperatures:");
      Console.WriteLine("Morning low - " + rs.Temperatures.MorningLow);
      Console.WriteLine("Daytime high - " + rs.Temperatures.DaytimeHigh);
      count++;
    }
    Console.WriteLine("------------------------------------------------------------------");
    Console.Write("ENTER to continue:");
    Console.ReadLine();

ForecastResult is the parent node within the returned XML for the description, date and temperatures etc. Because the forecast is for 7 days, each day is enclosed within a forecast node (rs) which we have to loop through in order to output the content.

jezzipin
  • 4,110
  • 14
  • 50
  • 94