1. In Visual Studio, add a web reference in the project explorer.
2. Create an class describing an object that represents the web service.
1
2 public class CurrentWeather
3 {
4
5
6
7 private String location;
8 private String time;
9 private String wind;
10 private String visibility;
11 private String temperature;
12 private String dewPoint;
13 private String relativeHumidity;
14 private String pressure;
15 private String status;
16
17
18
19
20
21
22 public String Location { get { return this.location; } set { this.location = value; } }
23 public String Time { get { return this.time; } set { this.time = value; } }
24 public String Wind { get { return this.wind; } set { this.wind = value; } }
25 public String Visibility { get { return this.visibility; } set { this.visibility = value; } }
26 public String Temperature { get { return this.temperature; } set { this.temperature = value; } }
27 public String DewPoint { get { return this.dewPoint; } set { this.dewPoint = value; } }
28 public String RelativeHumidity { get { return this.relativeHumidity; } set { this.relativeHumidity = value; } }
29 public String Pressure { get { return this.pressure; } set { this.pressure = value; } }
30 public String Status { get { return this.status; } set { this.status = value; } }
31
32
33
34 }
Use a method like this to call the service
1
2 private void btRead_Click(object sender, EventArgs e)
3 {
4 net.webservice.www.MyWeather gw = new net.webservice.www.MyWeather ();
5 this.lblShowStatus.Text = "Accessing Service";
6 this.Refresh();
7
8 Objects.CurrentWeather cwo = new Objects.CurrentWeather();
9 System.Xml.Serialization.XmlSerializer xs =
10 new System.Xml.Serialization.XmlSerializer(typeof(Objects.CurrentWeather));
11
12 // Deserialize
13 try
14 {
15 System.IO.TextReader tr = new System.IO.StringReader(gw.GetWeather(this.tbCity.Text, this.tbCountry.Text));
16 cwo = (Objects.CurrentWeather)xs.Deserialize(tr);
17 this.lblShowStatus.Text = cwo.Status;
18 this.lbShowHumidity.Text = cwo.RelativeHumidity;
19 this.lbShowPressure.Text = cwo.Pressure;
20 this.lbShowWind.Text = cwo.Wind;
21 this.lbShowTemperature.Text = cwo.Temperature;
22 }
23 catch
24 {
25 this.lblShowStatus.Text = String.Empty;
26 this.lbShowHumidity.Text = String.Empty;
27 this.lbShowPressure.Text = String.Empty;
28 this.lbShowWind.Text = String.Empty;
29 this.lbShowTemperature.Text = String.Empty;
30 this.lblShowStatus.Text = "Error";
31 }
32 }
33
34
35