我正在尝试访问位于 this link 的 Web 服务
如果您调用该方法,您将得到响应。
现在我想从我的 asp.net 应用程序调用 GetCitiesByCountry 方法,并仅将城市加载到下拉列表中,我编写了以下代码
protected void Page_Load(object sender, EventArgs e)
{
weather.GlobalWeatherSoapClient wet = new weather.GlobalWeatherSoapClient();
DataSet ds = new DataSet(wet.GetCitiesByCountry("United Kingdom"));
ds.GetXml();
DropDownList1.DataTextField = "City";
DropDownList1.DataValueField = "City";
DropDownList1.DataSource = ds;
DropDownList1.DataBind();
}
但我收到以下错误:
“/”应用程序中出现服务器错误。
IListSource 不包含任何数据源。
描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
异常详细信息:System.Web.HttpException:IListSource 不包含任何数据源。
Source Error:
Line 23: DropDownList1.DataValueField = "City";
Line 24: DropDownList1.DataSource = ds;
Line 25: DropDownList1.DataBind();
源文件:c:\Users\Rafi\Documents\Visual Studio 2013\Projects\WebSite4\demo1\main.aspx.cs 行:25
任何帮助将不胜感激。
最佳答案
您使用了错误的 DataSet 函数
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(wet.GetCitiesByCountry("United Kingdom")));
参见Msdn
关于c# - 如何从 Web 服务获取 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307708/