用WCF服务来动态的获取本地XML省市区文档

建立一个WCF服务。

  1 using ClassLibrary;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Runtime.Serialization;
  6 using System.ServiceModel;
  7 using System.ServiceModel.Web;
  8 using System.Text;
  9 using System.Xml;
 10
 11 namespace AreaService
 12 {
 13     // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“Service1”。
 14     // 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开始调试。
 15     public class Service1 : IService1
 16     {
 17         public static class api
 18         {
 19             public static XmlDocument cacheAreaDocument { get; set; }
 20             public static XmlDocument cacheUrlMapDocument { get; set; }
 21         }
 22         public List<ClassLibrary.Area> GetProvince()
 23         {
 24             XmlDocument xd = api.cacheAreaDocument;
 25             if (xd == null)
 26             {
 27                 xd = new XmlDocument();
 28                 xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
 29             }
 30             XmlNode xn = xd.SelectSingleNode("area");
 31             List<Area> areas = new List<Area>();
 32             //等效于 XmlNode xn = xd.DocumentElement;
 33             foreach (XmlNode province in xn.ChildNodes)
 34             {
 35                 XmlElement proEl = (XmlElement)province;
 36                 areas.Add(new Area { province = proEl.GetAttribute("province"), provinceID = proEl.GetAttribute("provinceID") });
 37             }
 38             return areas;
 39         }
 40         public List<ClassLibrary.Area> GetCityByProvinceId(string provinceID)
 41         {
 42             XmlDocument xd = api.cacheAreaDocument;
 43             if (xd == null)
 44             {
 45                 xd = new XmlDocument();
 46                 xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
 47             }
 48             XmlNode xn = xd.SelectSingleNode("area");
 49             List<ClassLibrary.Area> areas = new List<ClassLibrary.Area>();
 50             foreach (XmlNode province in xn.ChildNodes)
 51             {
 52                 XmlElement pro = (XmlElement)province;
 53                 if (pro.GetAttribute("provinceID") == provinceID)
 54                 {
 55                     foreach (var cityNd in pro.ChildNodes)
 56                     {
 57                         XmlElement city = (XmlElement)cityNd;
 58                         areas.Add(new ClassLibrary.Area { City = city.GetAttribute("City"), CityID = city.GetAttribute("CityID") });
 59                     }
 60                 }
 61             }
 62             if (xn.ChildNodes == null || xn.ChildNodes.Count <= 0)
 63             {
 64                 areas.Add(new ClassLibrary.Area { City = "", CityID = "" });
 65             }
 66             return areas;
 67         }
 68
 69         public List<ClassLibrary.Area> GetPieceareaByCityID(string CityID)
 70         {
 71             List<Area> areas = new List<Area>();
 72             if (CityID.Trim() == "")
 73             {
 74                 areas.Add(new Area { Piecearea = "", PieceareaID = "" });
 75                 return areas;
 76             }
 77             XmlDocument xd = api.cacheAreaDocument;
 78             if (xd == null)
 79             {
 80                 xd = new XmlDocument();
 81                 xd.Load(AppDomain.CurrentDomain.BaseDirectory + "/data/ChinaArea.xml");
 82             }
 83             XmlNode xn = xd.SelectSingleNode("area");
 84
 85             //等效于 XmlNode xn = xd.DocumentElement;
 86             foreach (XmlNode province in xn.ChildNodes)
 87             {
 88                 foreach (XmlNode cityNd in province.ChildNodes)
 89                 {
 90                     if ((cityNd as XmlElement).GetAttribute("CityID") == CityID)
 91                     {
 92                         foreach (var pieceareaNd in cityNd.ChildNodes)
 93                         {
 94                             XmlElement piecearea = (XmlElement)pieceareaNd;
 95                             areas.Add(new Area { Piecearea = piecearea.GetAttribute("Piecearea"), PieceareaID = piecearea.GetAttribute("PieceareaID") });
 96                         }
 97                     }
 98
 99                 }
100             }
101             return areas;
102         }
103     }
104 }

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

实例化。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9
10 namespace 动态的获取省市
11 {
12     public partial class Form1 : Form
13     {
14         public Form1()
15         {
16             InitializeComponent();
17             sa = new AreaService.Service1Client();
18         }
19         AreaService.Service1Client sa;
20         private void Form1_Load(object sender, EventArgs e)
21         {
22             comboBox1.DataSource = sa.GetProvince();
23             comboBox1.DisplayMember = "province";
24             comboBox1.ValueMember = "provinceID";
25             comboBox1_SelectedIndexChanged(sender,e);
26             comboBox2_SelectedIndexChanged(sender, e);
27             comboBox3_SelectedIndexChanged(sender, e);
28
29         }
30         /// <summary>
31         /// 获取市
32         /// </summary>
33         /// <param name="sender"></param>
34         /// <param name="e"></param>
35         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
36         {
37             comboBox2.DataSource = sa.GetCityByProvinceId(comboBox1.SelectedValue+"");
38             comboBox2.DisplayMember = "City";
39             comboBox2.ValueMember = "CityID";
40         }
41
42         private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
43         {
44             comboBox3.DataSource = sa.GetPieceareaByCityID(comboBox2.SelectedValue+"");
45             comboBox3.DisplayMember = "Piecearea";
46             comboBox3.ValueMember = "PieceareaID";
47         }
48
49
50         private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
51         {
52
53         }
54
55         private void button1_Click(object sender, EventArgs e)
56         {
57
58         }
59
60
61     }
62 }

这是窗体设计代码:

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;
    }
}

效果如下:

时间: 2024-10-24 07:01:41

用WCF服务来动态的获取本地XML省市区文档的相关文章

[笔记&amp;轮子]java源码 生成本地javadoc api文档

在用Eclipse写java代码时候,有时候因为不知道一个java函数的作用,会通过把鼠移动到java函数上,如果它有javadoc的相关内容就会显示出来.但是并非所有java代码都有javadoc:即使安装了javadoc,在eclipse中如果不进行设定,也可能无法使用. 我在win7下安装的是javase的jdk,发现eclipse中默认的javadoc路径是http://download.oracle.com/javase/7/docs/api/,显然这是一个在线资源,问题是网络总是不稳

鼠标相对于屏幕的位置、鼠标相对于窗口的位置和获取鼠标相对于文档的位置

一.screenX | screenY用于获取鼠标相对屏幕的位置在IE9+和其他主流浏览器,获取鼠标相对屏幕的位置,代码如下:function (ev){   ev.screenX //获取鼠标相对于屏幕左边的距离   ev.screenY //获取鼠标相对于屏幕顶部的距离}在IE浏览器下,获取鼠标相对屏幕的位置,代码如下:function(){   window.event.screenX //获取鼠标相对于屏幕左边的距离   window.event.screenY //获取鼠标相对于屏幕顶

mongoDB 获取最后插入的文档的ObjectID/_id方法

http://stackoverflow.com/questions/3338999/get-id-of-last-inserted-document-in-a-mongodb-w-java-driver mongoDB api就可以实现,请仔细查看集合insert方法的源代码 a.文档插入后可以获取到插入的文档的ObjectID 代码: BasicDBObject doc = new BasicDBObject( "name", "Matt" ); collect

【Unity3D】【NGUI】本地生成API文档

原地址:http://blog.csdn.net/u012091672/article/details/17438135 NGUI讨论群:333417608 1.安装Doxygen(http://www.stack.nl/~dimitri/doxygen/) 2.配置 1)工程名 2)版本 3)源代码目录(根据自己的修改) 4)递归扫描(包含子目录) 5)输出目录 1)只输出可以被文档化的实体 2)c#文档优化 1)有导航栏 2)不使用排版 1)随便指定个目录(用来保存配额) 2)切换到运行界面

xpages获取视图选择的文档

为了刚接触xpages的朋友写一些文档,这是以前收集的,以后会陆续把一些资料上来,请关注.在视图在怎么获取选择的文档,以及获取后怎么处理,这是非常实用的功能,但在帮助或百度是难找到的,所以与大家一起分享. var docids = getComponent("viewPanel1").getSelectedIds() //通過getComponent("viewPanel1").getSelectedIds()得到選擇文檔的ID, //viewPanel1为视图的名

本地开发spark代码上传spark集群服务并运行(基于spark官网文档)

打开IDEA 在src下的main下的scala下右击创建一个scala类 名字为SimpleApp ,内容如下 import org.apache.spark.SparkContext import org.apache.spark.SparkContext._ import org.apache.spark.SparkConf object SimpleApp { def main(args: Array[String]) { val logFile = "/home/spark/opt/s

使用WisdomTool RESTClient如何在Linux和Mac上获取测试报告和API文档?

使用WisdomTool RESTClient自动化测试REST API,生成REST API文档, 需要先执行命令java -jar restclient-1.1.jar启动WisdomTool RESTClient界面. 先使用工具测试REST API,产生历史记录. 选择菜单选项 Apidoc --> Create 生成API文档: work/apidoc/apidoc.html 选择菜单选项 Test --> Start Test 生成测试报告: work/report/report.

WCF分布式开发步步为赢(3)WCF服务元数据交换、配置及编程开发

今天我们继续WCF分布式开发步步为赢(3)WCF服务元数据交换.配置及编程开发的学习.经过前面两节的学习,我们了解WCF分布式开发的相关的基本的概念和自定义宿主托管服务的完整的开发和配置过程.今天我们来详细学习WCF服务元数据交换的相关内容.WCF服务元数据究竟是什么?为什么WCF服务要暴露元数据交换节点?这些和以前的Web Service有什么关系?WCF服务元数据交换的方式有那些?我们如何实现WCF服务元数据交换,本节我们会详细讲解.全文结构如下:[1]WCF服务元数据的基本概念.[2]WC

编写WCF服务时右击配置文件无“Edit WCF Configuration”(编辑 WCF 配置)远程的解决办法

原文:编写WCF服务时右击配置文件无“Edit WCF Configuration”远程的解决办法 今天在看<WCF揭秘>书中看到作者提出可以在一个WCF Host应用程序的App.Config文件上右击, 通过弹出的" Edit WCF Configuration”(编辑WCF配置)选项来利用GUI界面编辑WCF的配置信息. 但是我在尝试的时候并没有找到这个右键菜单,开始还以为作者弄错了,但又尝试了一会后便发现了窍门. 右键App.Config文件默认是没有" Edit