用WCF服务来动态的获取本地XML省市区文档
建立一个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;
}
}
效果如下:

用WCF服务来动态的获取本地XML省市区文档的更多相关文章
- GIT-查看本地html帮助文档
GIT安装后可以直接在命令行中通过指令调取本地html帮助文档如下所示: 格式: git help remote--git help 指令名称 git help remote 显示结果 获取branc ...
- mongoDB 获取最后插入的文档的ObjectID/_id方法
http://stackoverflow.com/questions/3338999/get-id-of-last-inserted-document-in-a-mongodb-w-java-driv ...
- 倒排索引 获取指定单词的文档集合 使用hash去重单词term 提高数据压缩率的方法
倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inve ...
- 创建一个简单的WCF程序2——手动开启/关闭WCF服务与动态调用WCF地址
一.创建WCF服务器 1.创建WCF服务器的窗体应用程序 打开VS2010,选择文件→新建→项目菜单项,在打开的新建项目对话框中,依次选择Visual C#→Windows→Windows窗体应用程序 ...
- 解决本地访问Android文档是非常慢的问题
不时在天上不能上网Android开发站点.要查看开发者文档,真是费劲心思,这里不再介绍访问Android开发网站developer.android.com,这里介绍怎样高速的訪问打开本地的SDK下An ...
- 获取打开的Word文档
using Word = Microsoft.Office.Interop.Word; int _getApplicationErrorCount=0; bool _isMsOffice = true ...
- mysql数据库基本操作以及获取数据库强大帮助文档
MySQL数据库强大帮助文档 mysql 中help等价于\h或者? mysql> ? create database;(查看创建数据库的语法) mysql> ? drop databas ...
- js和jQuery获取各种屏幕或文档的高度和宽度
1.jQuery获取文档或屏幕的高度 console.log($(window).height());//浏览器页面当前屏幕可见区域的高度 console.log($(document).height ...
- iOS - 开发中加载本地word/pdf文档说明
最近项目中要加载一个本地的word/pdf等文件比如<用户隐私政策><用户注册说明>,有两种方法加载 > 用QLPreviewController控制器实现 步骤 : & ...
随机推荐
- 微信小程序开发系列五:微信小程序中如何响应用户输入事件
微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 微信小程序开发系列三:微信小程序的调试方法 微信小程序开发系列四:微信小程序 ...
- 响应式布局(CSS3弹性盒flex布局模型)
传统的布局方式都是基于盒模型的 利用display.position.float来布局有一定局限性 比如说实现自适应垂直居中 随着响应式布局的流行,CSS3引入了更加灵活的弹性布局模型 flex弹性布 ...
- 联玛客(T 面试)
我看你写的项目都是SSM架构,那我们就来聊下Spring 1.Spring的生命周期,与生命周期相关的事件? 2.阿里巴巴开发手册中的规范有哪些? 切到了异常捕捉话题 3.线程你有了解吗? 创建线程的 ...
- DROP TABLE - 删除一个表
SYNOPSIS DROP TABLE name [, ...] [ CASCADE | RESTRICT ] DESCRIPTION 描述 DROP TABLE 从数据库中删除表或视图. 只有其所有 ...
- python之路——函数进阶
阅读目录 楔子 命名空间和作用域 函数嵌套及作用域链 函数名的本质 闭包 本章小结 楔子 假如有一个函数,实现返回两个数中的较大值: def my_max(x,y): m = x if x> ...
- PHP07 函数
学习要点 函数的定义 自定义函数 函数的工作原理和结构化编程 PHP变量范围 声明及应用各种形式的PHP函数 递归函数 使用自定义函数库 匿名函数和闭包 常用PHP系统函数 PHP7函数新特性 函数的 ...
- IntelliJ IDEA集成工具Database连接MySQL8.0报错的解决方法
直接用默认配置连接的话,会报以下错误: Connection to MySQL - @localhost failed. [08001] Could not create connection to ...
- 【简●解】[AHOI2009]中国象棋
[题目大意] 叫你在\(n×m\)的棋盘上放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,问有多少种放置方法. [关键词] \(DP\) 分类讨论 乘法和加法原理 [分析] 仔细观察就会发 ...
- LeetCoce 413. Arithmetic Slices
A sequence of number is called arithmetic if it consists of at least three elements and if the diffe ...
- 大数据学习——采集文件到HDFS
采集需求:比如业务系统使用log4j生成的日志,日志内容不断增加,需要把追加到日志文件中的数据实时采集到hdfs 根据需求,首先定义以下3大要素 l 采集源,即source——监控文件内容更新 : ...