使用 WSDL WebService 填充 ComboBox
Fill ComboBox with WSDL WebService
我使用此 Web 服务 http://www.webservicex.com/globalweather.asmx?WSDL 按国家/地区名称获取所有城市名称。我使用下面的代码来获得响应
GlobalWeatherReference.GlobalWeatherSoapClient weather = new GlobalWeatherReference.GlobalWeatherSoapClient("GlobalWeatherSoap12");
cities_cb.DataSource = weather.GetCitiesByCountry("Chad").ToList();
这个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>
现在我需要按城市名称填写组合框。请帮忙。
您需要获取响应并使用 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;
您可以基于 XML 架构生成 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; }
}
然后您需要使用 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();
}
我使用此 Web 服务 http://www.webservicex.com/globalweather.asmx?WSDL 按国家/地区名称获取所有城市名称。我使用下面的代码来获得响应
GlobalWeatherReference.GlobalWeatherSoapClient weather = new GlobalWeatherReference.GlobalWeatherSoapClient("GlobalWeatherSoap12");
cities_cb.DataSource = weather.GetCitiesByCountry("Chad").ToList();
这个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>
现在我需要按城市名称填写组合框。请帮忙。
您需要获取响应并使用 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;
您可以基于 XML 架构生成 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; }
}
然后您需要使用 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();
}