建立一个WCF服务。

 using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml; namespace AreaService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
public class Service1 : IService1
{
public static class api
{
public static XmlDocument cacheAreaDocument { get; set; }
public static XmlDocument cacheUrlMapDocument { get; set; }
}
public List<ClassLibrary.Area> GetProvince()
{
XmlDocument xd = api.cacheAreaDocument;
if (xd == null)
{
xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
}
XmlNode xn = xd.SelectSingleNode("area");
List<Area> areas = new List<Area>();
//等效于 XmlNode xn = xd.DocumentElement;
foreach (XmlNode province in xn.ChildNodes)
{
XmlElement proEl = (XmlElement)province;
areas.Add(new Area { province = proEl.GetAttribute("province"), provinceID = proEl.GetAttribute("provinceID") });
}
return areas;
}
public List<ClassLibrary.Area> GetCityByProvinceId(string provinceID)
{
XmlDocument xd = api.cacheAreaDocument;
if (xd == null)
{
xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
}
XmlNode xn = xd.SelectSingleNode("area");
List<ClassLibrary.Area> areas = new List<ClassLibrary.Area>();
foreach (XmlNode province in xn.ChildNodes)
{
XmlElement pro = (XmlElement)province;
if (pro.GetAttribute("provinceID") == provinceID)
{
foreach (var cityNd in pro.ChildNodes)
{
XmlElement city = (XmlElement)cityNd;
areas.Add(new ClassLibrary.Area { City = city.GetAttribute("City"), CityID = city.GetAttribute("CityID") });
}
}
}
if (xn.ChildNodes == null || xn.ChildNodes.Count <= )
{
areas.Add(new ClassLibrary.Area { City = "", CityID = "" });
}
return areas;
} public List<ClassLibrary.Area> GetPieceareaByCityID(string CityID)
{
List<Area> areas = new List<Area>();
if (CityID.Trim() == "")
{
areas.Add(new Area { Piecearea = "", PieceareaID = "" });
return areas;
}
XmlDocument xd = api.cacheAreaDocument;
if (xd == null)
{
xd = new XmlDocument();
xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
}
XmlNode xn = xd.SelectSingleNode("area"); //等效于 XmlNode xn = xd.DocumentElement;
foreach (XmlNode province in xn.ChildNodes)
{
foreach (XmlNode cityNd in province.ChildNodes)
{
if ((cityNd as XmlElement).GetAttribute("CityID") == CityID)
{
foreach (var pieceareaNd in cityNd.ChildNodes)
{
XmlElement piecearea = (XmlElement)pieceareaNd;
areas.Add(new Area { Piecearea = piecearea.GetAttribute("Piecearea"), PieceareaID = piecearea.GetAttribute("PieceareaID") });
}
} }
}
return areas;
}
}
}

建立后生成。然后在前端添加服务引用。

实例化。

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace 动态的获取省市
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sa = new AreaService.Service1Client();
}
AreaService.Service1Client sa;
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DataSource = sa.GetProvince();
comboBox1.DisplayMember = "province";
comboBox1.ValueMember = "provinceID";
comboBox1_SelectedIndexChanged(sender,e);
comboBox2_SelectedIndexChanged(sender, e);
comboBox3_SelectedIndexChanged(sender, e); }
/// <summary>
/// 获取市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.DataSource = sa.GetCityByProvinceId(comboBox1.SelectedValue+"");
comboBox2.DisplayMember = "City";
comboBox2.ValueMember = "CityID";
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox3.DataSource = sa.GetPieceareaByCityID(comboBox2.SelectedValue+"");
comboBox3.DisplayMember = "Piecearea";
comboBox3.ValueMember = "PieceareaID";
} private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{ } }
}

这是窗体设计代码:

namespace 动态的获取省市
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox3 = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(40, 50);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(29, 12);
this.label1.TabIndex = 0;
this.label1.Text = "省:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(40, 111);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(29, 12);
this.label2.TabIndex = 1;
this.label2.Text = "市:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(40, 175);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(29, 12);
this.label3.TabIndex = 2;
this.label3.Text = "区:";
//
// comboBox1
//
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(87, 47);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 20);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// comboBox2
//
this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(87, 108);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 20);
this.comboBox2.TabIndex = 4;
this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox2_SelectedIndexChanged);
//
// comboBox3
//
this.comboBox3.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox3.FormattingEnabled = true;
this.comboBox3.Location = new System.Drawing.Point(87, 172);
this.comboBox3.Name = "comboBox3";
this.comboBox3.Size = new System.Drawing.Size(121, 20);
this.comboBox3.TabIndex = 5;
this.comboBox3.SelectedIndexChanged += new System.EventHandler(this.comboBox3_SelectedIndexChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(87, 226);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 6;
this.button1.Text = "确 定";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(261, 272);
this.Controls.Add(this.button1);
this.Controls.Add(this.comboBox3);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.ComboBox comboBox3;
private System.Windows.Forms.Button button1;
}
}

效果如下:

最新文章

  1. 解决java.io.IOException: HTTPS hostname wrong: should be
  2. 策划了个.NET控件的案例大赛
  3. struts2从2.2.3升级到2.3.15.1步骤
  4. Python学习基础教程(learning Python)--2.2.1 Python下的变量解析
  5. 如何导入hadoop源码到eclipse
  6. Linux常用命令操作说明(链接)
  7. 【转】如何在IOS中使用3D UI - CALayer的透视投影
  8. cglib源码分析(二):Class name 生成策略
  9. Hadoop学习笔记-HDFS命令
  10. Net Core WebAPI
  11. TCP/IP详细解释--TCP/IP可靠的原则 推拉窗 拥塞窗口
  12. 发展合作-ASP.Net传递页面之间的值
  13. XCODE 控件连接(关联)不上变量 怎么解决
  14. uml视频系列(二)——uml的概述
  15. Ecshop去掉模版中随机出现Ecshop版权的方法
  16. go学习(二)目录管理
  17. 使用Navicat快速生成MySQL数据字典
  18. 【Nim游戏】高僧斗法
  19. SP687 REPEATS - Repeats
  20. mobile_基础事件

热门文章

  1. JS正则表达式匹配&lt;div&gt;&lt;style&gt;标签
  2. 在LXC Centos6-moban 编译安装mysql-5.6.36 时候遇见的报错
  3. zabbix 报警通知选项配置
  4. bind的使用
  5. MySQL系列(三)--数据库结构优化
  6. Microsoft Windows Server 系统基本配置
  7. JavaSE-26 Swing
  8. 《3+1团队》【Alpha】Scrum meeting 1
  9. 服务器编程心得(四)—— 如何将socket设置为非阻塞模式
  10. luogu P1008 三连击