【发布时间】:2015-04-09 19:41:23
【问题描述】:
猜测这是基本的东西,但我无法弄清楚。我正在使用一个 Web 服务,它返回下面的 XML 显示,但到目前为止我只处理了只返回一个字符串/int 等的 Web 方法。我不知道如何处理这个被返回的问题。它是 cdyne 天气网络服务,对于像我这样的菜鸟没有深入的例子。
<?xml version="1.0" encoding="UTF-8"?>
-<ForecastReturn xmlns="http://ws.cdyne.com/WeatherWS/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Success>true</Success>
<ResponseText>City Found</ResponseText>
<State>NY</State>
<City>New York</City>
<WeatherStationCity>White Plains</WeatherStationCity>
-<ForecastResult>
-<Forecast>
<Date>2014-09-20T00:00:00</Date>
<WeatherID>2</WeatherID>
<Desciption>Partly Cloudy</Desciption>
-<Temperatures>
<MorningLow>52</MorningLow>
<DaytimeHigh>73</DaytimeHigh>
</Temperatures>
-<ProbabilityOfPrecipiation>
<Nighttime>00</Nighttime>
<Daytime>10</Daytime>
</ProbabilityOfPrecipiation>
</Forecast>
-<Forecast>
<Date>2014-09-21T00:00:00</Date>
<WeatherID>3</WeatherID>
<Desciption>Mostly Cloudy</Desciption>
-<Temperatures>
<MorningLow>63</MorningLow>
<DaytimeHigh>78</DaytimeHigh>
</Temperatures>
-<ProbabilityOfPrecipiation>
<Nighttime>10</Nighttime>
<Daytime>20</Daytime>
</ProbabilityOfPrecipiation>
</Forecast>
</ForecastResult>
</ForecastReturn>
现在我只需要第一次预测中的描述来粘贴标签。
weatherWebService.Weather weatherService = new weatherWebService.Weather();
private void btnGo_Click(object sender, EventArgs e)
{
weatherService.GetCityForecastByZIP(txtZip.Text);
lblDescription.Text = magicVariableX;
}
该服务已被很好地添加并且可以调用,我只是不知道返回了什么,因为即使在浏览器中测试它的单个变量 string.int 被发回时,它也会显示为 XML,但是在代码中没有这样处理。
【问题讨论】:
-
您是否熟悉使用调试器..?您还可以加载返回到 DataTable 或 DataSet 中的 xml,并弄清楚那是什么。如果您不熟悉如何阅读 XML,互联网是一个广泛使用不足的工具。做一个简单的谷歌搜索关于如何使用 C# 读取 xml.. 另外,如果您知道 Web 服务的返回类型,您可以执行类似
var xmlData = weatherService.GetCityForecastByZIP(txtZip.Text);的操作,也不要发布无法编译的代码,例如lblDescription.Text = magicVariableX;什么是magicVariableX
标签: c# xml web-services return