C#编写街道管理系统
项目需求: 一、语言和环境 |
|
A、实现语言 |
|
C# |
|
B、环境要求 |
|
Visual Studio 2012 二、功能要求 现使用.NET WinForms技术为居委会开发一个街道管理软件,其中街道管理窗体界面如图-4所示。
要求: 1、读取提供的XML文件(详见 \提供给学员的素材\Address.xml),将其信息动态添加到TreeView控件中。 2、创建表示地址信息的实体类,并使用泛型集合保存XML文件中的信息。 3、选中某子节点,能够将相关信息显示到右侧的文本框中,如图-4所示。 4、窗体标题为“街道管理”。
|
XML文件:
<?xml version="1.0" encoding="gb2312"?> <root name="石牌街道"> <juwei name="瑞华居委"> <jianzhu smid="6" name="瑞华花园1栋"> <fjname>101室</fjname> <fjname>102室</fjname> </jianzhu> <jianzhu smid="7" name="瑞华花园2栋"> <fjname>101室</fjname> <fjname>102室</fjname> </jianzhu> </juwei> <juwei name="金帝居委"> <jianzhu smid="4" name="金帝花园1栋"> <fjname>101室</fjname> <fjname>102室</fjname> </jianzhu> </juwei> <juwei name="朝阳居委"> <jianzhu smid="5" name="朝阳花园1栋"> <fjname>101室</fjname> <fjname>102室</fjname> </jianzhu> </juwei> </root>
项目类图:
存储街道数据类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Street_Management_System { public class Street { public Street() { } public Street(string streets, string jwh, string dong, string s) { Streets = streets; Jwh = jwh; Dong = dong; S = s; } //街道 public string Streets { get; set; } //居委会 public string Jwh { get; set; } //楼房(栋) public string Dong { get; set; } //厅室 public string S { get; set; } } }
读取XML类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Street_Management_System { public class ReadXML { public ReadXML() { dic = new Dictionary<string, Street>(); } public ReadXML(Dictionary<string, Street> dic) { this.dic = dic; } //存储xml文件的双列集合 public Dictionary<string, Street> dic { get; set; } public Dictionary<string, Street> Read() { XmlDocument doc = new XmlDocument(); /* * 读取xml文件 * */ doc.Load("Address.xml"); //获取xml文件的根节点 XmlNode xml = doc.DocumentElement; /* * 解析xml文件的各个接点并存入集合 * */ foreach (XmlNode item in xml.ChildNodes) { string street = item.ParentNode.Attributes["name"].InnerText; string jwh = item.Attributes["name"].InnerText; string dong = null; string s = null; foreach (XmlNode items in item.ChildNodes) { dong = items.Attributes["name"].InnerText; foreach (XmlNode itemss in items.ChildNodes) { s = itemss.InnerText; Street aaa = new Street(street,jwh,dong,s); dic.Add(dong+"|"+s,aaa); } } } return dic; } } }
主窗体类:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Street_Management_System { public partial class 街道管理系统 : Form { public 街道管理系统() { InitializeComponent(); } public Dictionary<string, Street> dic = new Dictionary<string, Street>(); private void Form1_Load(object sender, EventArgs e) { AddTree(); } /* * 接收ReadXML类中存储xml信息的集合 * */ public void AddTree() { ReadXML xml = new ReadXML(); dic = xml.Read(); string street = null; string jwh = null; string dong = null; TreeNode sTree = null; TreeNode streetTree = null; TreeNode jwhTree = null; TreeNode dongTree = null; /* *拆解集合中的数据并赋值到TreeView控件的相应接点中 * */ foreach (KeyValuePair<string, Street> item in dic) { if (street != item.Value.Streets) { streetTree = new TreeNode(); streetTree.Text = item.Value.Streets; tvStreet.Nodes.Add(streetTree); } if (jwh != item.Value.Jwh) { jwhTree = new TreeNode(); jwhTree.Text = item.Value.Jwh; streetTree.Nodes.Add(jwhTree); } if (dong != item.Value.Dong) { dongTree = new TreeNode(); dongTree.Text = item.Value.Dong; jwhTree.Nodes.Add(dongTree); } sTree = new TreeNode(); sTree.Text = item.Value.S; sTree.Tag = item.Key; dongTree.Nodes.Add(sTree); street = item.Value.Streets; jwh = item.Value.Jwh; dong = item.Value.Dong; } } private void tvStreet_AfterSelect(object sender, TreeViewEventArgs e) { /* * 当点击节点时,读取选中节点的信息赋值到文本框中显示 * */ if (tvStreet.SelectedNode.Level == 0) { textBox1.Text = tvStreet.SelectedNode.Text; } else if (tvStreet.SelectedNode.Level == 1) { textBox2.Text = tvStreet.SelectedNode.Text; } else if (tvStreet.SelectedNode.Level == 2) { textBox3.Text = tvStreet.SelectedNode.Text; } else if (tvStreet.SelectedNode.Level == 3) { textBox4.Text = tvStreet.SelectedNode.Text; } } } }