public class Province { private string id; /// /// 编号 /// public string Id { get { return id; } set { id = value; } } private string name; /// /// 名称 /// public string Name { get { return name; } set { name = value; } } }
对应City类:
代码如下:
public class City { private string id; /// /// 编号 /// public string Id { get { return id; } set { id = value; } } private string name; /// /// 名称 /// public string Name { get { return name; } set { name = value; } } }
对应county类:
代码如下:
public class County { private string id; /// /// 编号 /// public string Id { get { return id; } set { id = value; } } private string name; /// /// 名称 /// public string Name { get { return name; } set { name = value; } } }
第三步:编写服务器端处理程序类:Handler.cs
代码如下:
/// 2 /// 处理程序 3 /// 4 public class Handler : IHttpHandler 5 { 6 7 private static XDocument doc; 8 private string filePath = HttpContext.Current.Server.MapPath("~/Area.xml"); 9 //Javascript序列化类 private static JavascriptSerializer jss = new JavascriptSerializer(); public void ProcessRequest(HttpContext context) { context.Response.COntentType= "text/plain"; string result = "failure";//默认返回结果为失败 HttpRequest req = context.Request; string province = req["province"];//获取用户选择的省的编号 string city = req["city"];//获取用户选择的市的编号 string county = req["county"];//获取用户选择的县的编号 string type = req["type"];//获取用户需要获取的省市县列表的类型 InitDoc(); if (type.HasValue()) { switch (type.ToLower()) { case "province"://如果用户需要获取省级列表 result = jss.Serialize(GetProvinceList()); break; case "city"://如果用户需要获取的是市级列表 result = jss.Serialize(GetCityListByProvince(province)); break; case "county"://如果用户需要获取的是县级列表 result = jss.Serialize(GetCountyListByCity(province, city)); break; default: break; } } //将结果以文本的格式返回给客户端 context.Response.Write(result); } /// /// 初始化文档对象 /// private void InitDoc() { if (doc == null) { doc = XDocument.Load(filePath); } } /// /// 初始化省级列表 /// private List GetProvinceList() { List list = new List(); if (doc != null) { XElement root = doc.Root; foreach (var prov in root.XPathSelectElements("province")) { list.Add(new Province() { Id = prov.Attribute("id").Value, Name = prov.Attribute("name").Value }); } } return list; } /// /// 根据省级编号获取市级编号 /// /// 省级编号 private List GetCityListByProvince(string provId) { List list = new List(); if (doc != null) { XElement root = doc.Root; //xpath表达式:/area/province[@id='1']/city string queryPath = "/area/province[@id='" + provId + "']/city"; foreach (var city in root.XPathSelectElements(queryPath)) { list.Add(new City() { Id = city.Attribute("id").Value, Name = city.Attribute("name").Value }); } } return list; } /// /// 根据省级编号和市级编号获取县级编号 /// /// 省级编号 /// 市级编号 private List GetCountyListByCity(string provId, string cityId) { List list = new List(); if (doc != null) { XElement root = doc.Root; string queryPath = "/area/province[@id='" + provId + "']/city[@id='" + cityId + "']/county"; foreach (var county in root.XPathSelectElements(queryPath)) { list.Add(new County() { Id = county.Attribute("id").Value, Name = county.Attribute("name").Value }); } } return list; } public bool IsReusable { get { return false; } } }