2017-09-16 8 views
0

私はこのWebサービスhttp://www.webservicex.com/globalweather.asmx?WSDLを使用して、すべての都市名を国名で取得します。私はこれが今、私は都市名でコンボボックスを埋めるために必要コンボボックスにWSDL WebServiceを入力してください

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> 

を返す応答

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

を取得するためのコードの下に使用します。助けてください。

答えて

2

あなたは、このウェブリソース使用例えばXMLスキーマに基づいてC#クラスを生成することができ

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; 
1

、応答を取得し、以下のようにStringReaderを使用する必要があります:あなたはこれらのクラスを取得した後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(); 
     } 
関連する問題