在C#控制台应用程序中调用web服务后访问响应节点

本文关键字:服务 访问 响应 节点 web 调用 控制台 应用程序 | 更新日期: 2023-09-27 18:00:01

我目前正在使用C#学习.Net,并已开始尝试Web服务。在另一个线程上的人的帮助下:(定义使用Web服务时要使用的端点)我已经成功地整理了我的端点,并从我正在使用的Web服务中获得了某种形式的响应。我现在唯一的问题是,我收到的响应似乎是一个数组,我需要能够解析这个数组中的各个节点。

为了阐明这一点,以下是调用web服务返回的XML结构:

<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>

我希望能够通过名称或类似方式访问此响应中的节点。所以,如果我声明我想要温度,它将返回温度,无论使用结果如何。在这种情况下,温度似乎不起作用,这表明节点不会成为响应对象的属性。

这是我到目前为止的代码:

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.

我的问题是,如何指定对web服务的调用应该使用其中一个SOAP端点?到目前为止,我的代码可以在下面找到:

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();
        }
      }
    }
  }
}

我如何调整它以访问来自此web服务的响应中的不同节点?

如有任何帮助,我们将不胜感激。

在C#控制台应用程序中调用web服务后访问响应节点

在这个例子中,一些节点作为数组的一部分返回。数组是使用父容器引用的。因此,在我上面的例子中,我需要按名称访问节点:

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是返回的XML中描述、日期和温度等的父节点。因为预测是7天的,所以每天都包含在一个预测节点(rs)中,我们必须循环使用该节点才能输出内容。