简体   繁体   中英

How do I get XML from a Webservice?

I am trying to access web service located on this link

If you invoke the method you will get response.

now I wanted to call GetCitiesByCountry method from my asp.net application and load only cities into dropdownlist and I have coded following

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

But I am getting the following error:

Server Error in '/' Application.

The IListSource does not contain any data sources.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: The IListSource does not contain any data sources.

Source Error:     

Line 23: DropDownList1.DataValueField = "City";
Line 24: DropDownList1.DataSource = ds;
Line 25: DropDownList1.DataBind();          

Source File: c:\\Users\\Rafi\\Documents\\Visual Studio 2013\\Projects\\WebSite4\\demo1\\main.aspx.cs Line: 25


Any help would much appreciated.

You use the wrong DataSet functions

DataSet ds = new DataSet();
ds.ReadXml(new StringReader(wet.GetCitiesByCountry("United Kingdom")));

See Msdn

This is because you are setting the name of the dataset and not putting any data into it. The Dataset(string) constructor sets the name of the dataset to the string passed into it. So essentially you are setting the name of the dataset to the value returned by your web service.

You should do this

DataSet yourDataSet = new DataSet("my dataset");

yourDataSet.ReadXml(new StringReader(wet.GetCitiesByCountry("United Kingdom")));

Try

DropDownList1.DataSource = ds.Tables["<tablename>"].DefaultView;

here is the table name from which you needs to load the data in dropdownlist.

if you have only one table in your data set then you can do as below

DropDownList1.DataSource = ds.Tables[0].DefaultView;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM