简体   繁体   English

用WSDL WebService填充ComboBox

[英]Fill ComboBox with WSDL WebService

I use this web service http://www.webservicex.com/globalweather.asmx?WSDL to get all cities name by country name. 我使用此Web服务http://www.webservicex.com/globalweather.asmx?WSDL来按国家/地区名称获取所有城市的名称。 I use below code to get response 我使用下面的代码来获得响应

GlobalWeatherReference.GlobalWeatherSoapClient weather = new GlobalWeatherReference.GlobalWeatherSoapClient("GlobalWeatherSoap12");
            cities_cb.DataSource = weather.GetCitiesByCountry("Chad").ToList();

This returns 这返回

string

<NewDataSet>
  <Table>
    <Country>Chad</Country>
    <City>Sarh</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Abeche</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Moundou</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Ndjamena</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Bokoro</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Bol-Berim</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Am-Timan</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Pala</City>
  </Table>
  <Table>
    <Country>Chad</Country>
    <City>Faya</City>
  </Table>
</NewDataSet>

Now I need to fill combo box by cities name. 现在,我需要按城市名称填写组合框。 Please help. 请帮忙。

You need to get the response and use StringReader as below, 您需要获取响应并按如下所示使用StringReader

List<string> cityNames = new List<string>();
GlobalWeatherReference.GlobalWeatherSoapClient client = new GlobalWeatherReference.GlobalWeatherSoapClient("GlobalWeatherSoap12");
var allCountryCities = client.GetCitiesByCountry("Chad");
if (allCountryCities.ToString() == "Data Not Found")
{

}
DataSet ds = new DataSet();
//Creating a stringReader object with Xml Data
StringReader stringReader = new StringReader(allCountryCities);
// Xml Data is read and stored in the DataSet object
ds.ReadXml(stringReader);
//Adding all city names to the List objects
foreach (DataRow item in ds.Tables[0].Rows)
{
    cityNames.Add(item["City"].ToString());
}    
cities_cb.DataSource = cityNames;

you can generate C# classes based on XML Schema , for example using this web resource : http://xmltocsharp.azurewebsites.net/ after you get these classes : 您可以基于XML Schema生成C#类,例如,使用以下Web资源: http : //xmltocsharp.azurewebsites.net/

[XmlRoot(ElementName = "Table")]
public class Table
{
    [XmlElement(ElementName = "Country")]
    public string Country { get; set; }
    [XmlElement(ElementName = "City")]
    public string City { get; set; }
}

[XmlRoot(ElementName = "NewDataSet")]
public class NewDataSet
{
    [XmlElement(ElementName = "Table")]
    public List<Table> Table { get; set; }
}

then you need to deserialize response from WS using type of NewDataSet 那么您需要使用NewDataSet类型反序列化来自WS的响应

        GlobalWeatherSoapClient gwsc = new GlobalWeatherSoapClient("GlobalWeatherSoap12");
        var response = gwsc.GetCitiesByCountry("Chad");
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(NewDataSet));
        var dataSet = xmlSerializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(response))) as NewDataSet;
        if (dataSet != null)
        {
            var cities = dataSet.Table.Select(x => x.City).ToList();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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