【问题标题】:Reading XML or objects from a Web service从 Web 服务读取 XML 或对象
【发布时间】:2014-06-06 23:30:16
【问题描述】:

这是我第一次使用 Web 服务,我有点迷茫。 我成功调用了这些函数,但我只能从中获得一个值 服务。我读到最简单的方法是读取 xml 或创建对象 然后调用它们的值。目前我使用返回所需的函数 值,但我需要给他们打电话 3 次才能获得所有数据,女巫是浪费时间 和资源。我尝试使用 URL 调用服务并将其用作网站 或让服务以相同的方式工作而不导入程序。 问题是我找不到将值传递到 url 的方法,因为 我只得到空白页。从服务中获取我的数据的最快方法是什么? 如果城市有效,我需要城市名称、温度和旗帜。我需要通过拉链 服务代码。

谢谢。

我当前的代码

wetther.Weather wether = 新wetther.Weather();

        string farenhait = wether.GetCityWeatherByZIP(zip).Temperature;
        string city = wether.GetCityWeatherByZIP(zip).City;
        bool correct = wether.GetCityWeatherByZIP(zip).Success;

我试过了

// Retrieve XML document  
XmlTextReader reader = new XmlTextReader("http://xml.weather.yahoo.com/forecastrss?p=94704");  

// Skip non-significant whitespace  
reader.WhitespaceHandling = WhitespaceHandling.Significant;  

// Read nodes one at a time  
while (reader.Read())  
{  
    // Print out info on node  
    Console.WriteLine("{0}: {1}", reader.NodeType.ToString(), reader.Name);  
}  

这个适用于雅虎页面,但不适用于我的页面。

我需要使用这个网络服务 -> http://wsf.cdyne.com/WeatherWS/Weather.asmx

【问题讨论】:

  • 您正在使用 C# 中的 Web 服务,对吗?为什么不添加网络服务参考How to: Add a Reference to a Web Service
  • @Anuraj 实际上目前我正在这样做。我只是不知道如何获取xml。我只能获得每个连接的值。

标签: c# xml web-services


【解决方案1】:

最好的方法是将 Web 服务的引用添加到您的项目中。从这里您将能够像查询项目中的类一样查询 Web 服务。在Solution Explorer 中右键单击您的项目,然后单击Add Service Reference。然后,您可以将 Web 服务 URL 复制并粘贴到对话框中。

那你就可以这样查询了……

Weather.WeatherSoapClient w = new Weather.WeatherSoapClient();

Weather.ForecastReturn f = w.GetCityForecastByZIP("12345");

string farenhait = f.Temperature;
string city = f.City;
bool correct = f.Success;

基本上,您所做的是每次触发 HTTP 请求。您需要做的是将单个请求放入一个对象 (f),然后您可以检索其属性。

【讨论】:

  • 我就是这样做的。但后来我这样称呼它。 string farenhait = wether.GetCityWeatherByZIP(zip).Temperature;字符串城市 = wether.GetCityWeatherByZIP(zip).City;我应该怎么称呼它,我不需要调用它 2x ?
【解决方案2】:
WeatherServiceRef.WeatherSoapClient weatherSoapClient = new WeatherSoapClient("WeatherSoap");

WeatherServiceRef.ForecastReturn forecastRet = weatherSoapClient.GetCityForecastByZIP("90210"); //enter valid zip string

foreach (Forecast forecast in forecastRet.ForecastResult)    
    {
    Console.WriteLine("\nForecast {0}", forecast.WeatherID);
    Console.WriteLine ("Temperature (morning low): {0}", forecast.Temperatures.MorningLow);
    Console.WriteLine("Temperature (morning high): {0}", forecast.Temperatures.DaytimeHigh);
    Console.WriteLine("Probability of precipitation (daytime): {0}", forecast.ProbabilityOfPrecipiation.Daytime)
    //insert other code to retrieve values here
   }

Console.ReadLine();

您可以使用右键单击添加服务将服务引用添加到 Web 服务。在我的例子中,我调用了服务引用 Wea​​therServiceRef。

【讨论】:

    【解决方案3】:

    我使用了@MichaelCoxes awser 并想出了这个。完美运行

            degress.TempConvert todegress = new degress.TempConvert();
            wetther.Weather wether = new wetther.Weather();
    
            wetther.WeatherReturn f = wether.GetCityWeatherByZIP("10001");
    
            string city = f.City;
            bool correct = f.Success;
            string farenhait = f.Temperature;
    

    【讨论】:

      猜你喜欢
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-27
      • 1970-01-01
      相关资源
      最近更新 更多