1.创建服务引用
例如:天气预报
2.在代码添加引用空间
class="code_img_closed" src="/Upload/Images/2017120611/0015B68B3C38AA5B.gif" alt="">1 TvProgram.ChinaTVprogramWebService tp = new TvProgram.ChinaTVprogramWebService(); 2 DataSet ds= tp.getAreaDataSet();logs_code_collapse">View Code
3.引用具体方法
1 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 2 { 3 ComboBox cb = (ComboBox)sender; 4 if(comboBox1.SelectedIndex>0) 5 { 6 7 int num= Convert.ToInt32(cb.SelectedValue.ToString()); 8 9 TvProgram.ChinaTVprogramWebService tp = new TvProgram.ChinaTVprogramWebService(); 10 DataSet ds = tp.getTVstationDataSet(num); 11 comboBox2.DataSource = ds.Tables[0]; 12 comboBox2.DisplayMember = "tvStationName"; 13 comboBox2.ValueMember = "tvStationID"; 14 15 } 16 }
返回的xml用两种处理方法
//DataRow[] dt = ds.Tables[0].Select(); //for (int i = 3; i < dt.Length; i++) //{ // Text = dt[i]["Area"].ToString(); // Value = Convert.ToInt32(dt[i]["areaID"].ToString()); // comboBox1.Items.Add(Text); //}
comboBox1.DataSource = ds.Tables[0]; comboBox1.DisplayMember = "Area"; comboBox1.ValueMember = "areaID";