c# - 如何从 Web 服务获取 XML?

标签 c# asp.net web-services asmx

我正在尝试访问位于 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/

相关文章:

ruby-on-rails - 在本地主机 :4567 but not on IP 上运行的 Ruby Sinatra Web 服务

c# - ASP.NET 中缓存的 Request.IsAuthenticated 问题

asp.net - 在 dotnet mvc 5 中,如何将服务注入(inject) View ?

c# - 访问动态创建的控件的值 c# asp.net

javascript - 使用javascript控制文本框

java - netty ChannelInboundHandlerAdapter将帧切成约1500个字节

java - 存储在请求范围内 - 从 SOAPHandler 到 @Producer

c# - ILMerged 后控制台应用程序不再有控制台

c# - 如何异步执行 Azure 表存储查询?客户端版本 4.0.1

c# - 在 C# 中使用正则表达式删除括号