Web services Part 2: Coding Section

Select Go button in our form. Double click on it. You will get this.

private void button2_Click(object sender, EventArgs e)
{ }

This is nothing but the Click Event of button button2 (i.e go button)

Note: Go is the caption of button2.

Now write following code:

//net.webservicex.www.GlobalWeather is the class name
//GW is the object.

net.webservicex.www.GlobalWeather GW = new WindowsFormsApplication6.net.webservicex.www.GlobalWeather();
string temp1 = GW.GetWeather(txtCity.Text, txtCountry.Text);
if (temp1 != string.Empty)
button2.Enabled = false;

//get weather is method to get weather and returns string in Xml format

byte[] byteArray = Encoding.Unicode.GetBytes(temp1);
MemoryStream stream = new MemoryStream(byteArray);
XmlTextReader txt = new XmlTextReader(stream);
List w_data = new List();

//Above Code is used to read xml and extracting the element information

while (txt.Read())
if (txt.NodeType == XmlNodeType.Text)
string values = txt.Value;
w_data.Add(values); //information is added in list

//adding Values to the textboxes

txtLocation.Text = w_data[0];
this.txtTimeZone.Text = w_data[1];
this.txtwind.Text = w_data[2];
this.txtVis.Text = w_data[3];
this.txtSky.Text = w_data[4];
this.txttemp.Text = w_data[5];
this.txtDew.Text = w_data[6];
this.txthumid.Text = w_data[7];
this.txtpressure.Text = w_data[8];
button2.Enabled = true;
button2.Enabled = true;
MessageBox.Show("Error Reading Data.", "Status|Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
catch (Exception ex)
button2.Enabled = true;
MessageBox.Show(ex.Message +"\n"+ex.HelpLink);

This is my Custom sample application .You can download Demo application & this Custom Application from Download section.

Hope you will like this tutor

posted under , |


Newer Post Older Post Home

We have seen by



Recent Comments