添加联系人窗体:
修改联系人窗体:
以下是用于测试程序的XML文件:
contact.xml 将该文件保存在项目目录下
<?xml version="1.0" encoding="gb2312"?><ContactDetails><Contact> <name> <first>Steven</first> <last>Perez</last> </name> <note>CEONTALI@yahoo.com.cn;system at http://www.details.net/token</note></Contact><Contact> <name> <first>Billoys</first> <last>Perez</last> </name> <note>Billoys@163.com.cn;system at http://www.Billoys.com/Billoys.htm</note></Contact><Contact> <name> <first>刘</first> <last>罗锅</last> </name> <note>古代人</note></Contact></ContactDetails>
contact2.xml 该文件用于实现导入联系人功能,将该文件随便保存在一个目录下然后将保存路径连同文件名拷贝到主窗体的保存的路径文本框中再单击导入按纽即可实现导入功能。
<?xml version="1.0" encoding="gb2312"?><ContactDetails><Contact> <name> <first>Steven</first> <last>Perez</last> </name> <note>CEONTALI@yahoo.com.cn;system at http://www.details.net/token</note></Contact><Contact> <name> <first>Billoys</first> <last>Perez</last> </name> <note>Billoys@163.com.cn;system at http://www.Billoys.com/Billoys.htm</note></Contact><Contact> <name> <first>刘</first> <last>德华</last> </name> <note>香港著名艺人,工作勤恳同时不忘生活,出演电影100多部,演技已达登峰造极,刻画人物栩栩如生</note></Contact><Contact> <name> <first>扬</first> <last>震</last> </name> <note>重案六组探员,为人胆大心细,沉着冷静,富有人情味,经历几次案件后更加成熟,在成长中不断磨练,是个真的汉子,正应验那句话:成就靠真本事</note></Contact><Contact> <name> <first>季</first> <last>洁</last> </name> <note>重案六组探员,富有人情味,对扬震早已芳心默许,知道为什么吗?因为她天生就爱保护别人,当她看到扬震被别人用枪指着头吓的回不过神来时就对这个真实的男人产生了感觉,真可谓巾帼不让须眉。</Contact></ContactDetails>
导出联系人时在保存的路径文本框中输入一个文件路径,程序将在该路径下创建一个XML文件,如果该文件存在于该路径上,程序将对该XML文件进行重写。
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)为实现以上所述所有功能,我专门编写了一个类来封装实现代码,该类代码如下:
namespace ContactApplication{ using System; using System.Xml; using System.Text; using System.Data; using System.Windows.Forms; using System.ComponentModel; using System.Collections; /// summary /// Contact 联系人 /// /summary public class Contact : IDisposable { private string xmlPath; private XmlDocument xmlDoc; private XmlNode selectNode; private string firstName; private string lastName; private string note; #region Contact 构造器 /// summary /// 默认构造器 /// /summary public Contact() { this.xmlPath = "../../Contact.xml"; this.selectNode = null; this.xmlDoc = new XmlDocument(); this.xmlDoc.Load(this.xmlPath); this.firstName = string.Empty; this.lastName = string.Empty; this.note = string.Empty; } /// summary /// 使用姓氏,名字,个人信息构造一个联系人对象 /// /summary /// param name="firstName"姓氏/param /// param name="lastName"名字/param /// param name="note"个人信息/param public Contact(string firstName, string lastName, string note) { this.xmlPath = "../../Contact.xml"; this.selectNode = null; this.xmlDoc = new XmlDocument(); this.xmlDoc.Load(this.xmlPath); this.firstName = firstName; this.lastName = lastName; this.note = note; } #endregion #region Contact 资源释放方法 /// summary /// 清理该对象所有正在使用的资源 /// /summary public void Dispose() { this.Dispose(true); GC.SuppressFinalize(this); } /// summary /// 释放该对象的实例变量 /// /summary /// param name="disposing"/param protected virtual void Dispose(bool disposing) { if (!disposing) return; if (this.xmlPath != null) this.xmlPath = null; if (this.xmlDoc != null) this.xmlDoc = null; if (this.selectNode != null) this.selectNode = null; if (this.firstName != null) this.firstName = null; if (this.lastName != null) this.lastName = null; if (this.note != null) this.note = null; } #endregion #region Contact 属性 /// summary /// 姓氏 /// /summary public string FirstName { get { return this.firstName; } set { this.firstName = value; } } /// summary /// 名字 /// /summary public string LastName { get { return this.lastName; } set { this.lastName = value; } } /// summary /// 个人信息 /// /summary public string Note { get { return this.note; } set { this.note = value; } } #endregion #region Contact 功能函数 /// summary /// 加载所有的联系人姓名 /// /summary /// returns联系人姓名列表/returns public ArrayList LoadContactName() { ArrayList nameList= new ArrayList(); XmlNodeList nameNodeList = xmlDoc.SelectNodes("//name"); foreach(XmlNode nameNode in nameNodeList) { XmlElement firstNameNode = (XmlElement)nameNode.FirstChild; XmlText firstNameText = (XmlText)firstNameNode.FirstChild; XmlElement lastNameNode = (XmlElement)nameNode.LastChild; XmlText lastNameText = (XmlText)lastNameNode.FirstChild; nameList.Add(lastNameText.Value + " " + firstNameText.Value); } return nameList; } /// summary /// 获取note节点的文本值 /// /summary /// returnsnote节点的文本值/returns public string DisplayNote() { string note = string.Empty; XmlElement selectName = (XmlElement)xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{1}']",this.firstName,this.lastName)); note = selectName.NextSibling.InnerText; return note; } /// summary /// 搜索联系人 /// /summary /// remarks /// 根据传入的搜索类型(按姓或名)以及搜索值查询符合条件的联系人信息,并以对话框形式显示 /// /remarks /// param name="searchType"搜索类型(first或last)/param /// param name="searchValue"搜索值/param public void SearchContact(string searchType, string searchValue) { string contactInfo = string.Empty; int i = 0; XmlNodeList nameNodeList = xmlDoc.SelectNodes(string.Format("//name[{0}='{1}']",searchType,searchValue)); if(searchType == "first") { MessageBox.Show(string.Format("符合{0}姓氏的联系人有{1}个",searchValue,nameNodeList.Count),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information); if (nameNodeList.Count == 0) return; foreach(XmlNode nameNode in nameNodeList) { i++; contactInfo = nameNode.LastChild.InnerText + " " + nameNode.FirstChild.InnerText; contactInfo = contactInfo + System.Environment.NewLine + "====================" + System.Environment.NewLine + nameNode.NextSibling.InnerText; MessageBox.Show(string.Format("第{0}个联系人:{1}{2}{3}",i,System.Environment.NewLine,System.Environment.NewLine,contactInfo),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information); } } else if(searchType == "last") { MessageBox.Show(string.Format("符合{0}名字的联系人有{1}个",searchValue,nameNodeList.Count),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information); if (nameNodeList.Count == 0) return; foreach(XmlNode nameNode in nameNodeList) { i++; contactInfo = nameNode.LastChild.InnerText + " " + nameNode.FirstChild.InnerText; contactInfo = contactInfo + System.Environment.NewLine + "====================" + System.Environment.NewLine + nameNode.NextSibling.InnerText; MessageBox.Show(string.Format("第{0}个联系人:{1}{2}{3}",i,System.Environment.NewLine,System.Environment.NewLine,contactInfo),"搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information); } } else { MessageBox.Show("没有发现与您的搜索条件匹配的项,请检查您的操作是否正确!","搜索联系人",MessageBoxButtons.OK,MessageBoxIcon.Information); } } /// summary /// 添加联系人 /// /summary public void AddContact() { XmlDocumentFragment xmlDocFrag = xmlDoc.CreateDocumentFragment(); XmlElement contactEle = xmlDoc.CreateElement("Contact"); XmlElement nameEle = xmlDoc.CreateElement("name"); XmlElement firstNameEle = xmlDoc.CreateElement("first"); firstNameEle.InnerText = this.firstName; nameEle.AppendChild(firstNameEle); XmlElement lastNameEle = xmlDoc.CreateElement("last"); lastNameEle.InnerText = this.lastName; nameEle.AppendChild(lastNameEle); XmlElement noteEle = xmlDoc.CreateElement("note"); noteEle.InnerText = this.note; contactEle.AppendChild(nameEle); contactEle.AppendChild(noteEle); xmlDocFrag.AppendChild(contactEle); XmlElement detailsEle = (XmlElement)xmlDoc.SelectSingleNode("/ContactDetails"); detailsEle.AppendChild(xmlDocFrag.FirstChild); xmlDoc.Save(this.xmlPath); } /// summary /// 修改联系人 /// /summary public void UpdateContact() { selectNode.FirstChild.InnerText = this.firstName; selectNode.LastChild.InnerText = this.lastName; selectNode.NextSibling.InnerText = this.note; xmlDoc.Save(this.xmlPath); } /// summary /// 删除联系人 /// /summary public void DeleteContact() { XmlElement contactEle = (XmlElement)this.selectNode.ParentNode; XmlElement detailsEle = (XmlElement)contactEle.ParentNode; detailsEle.RemoveChild(contactEle); xmlDoc.Save(this.xmlPath); } /// summary /// 根据列表框中选中的联系人在文档中选择一个对应的联系人 /// /summary public void SelectContact() { this.selectNode = xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{1}']",this.firstName,this.lastName)); } /// summary /// 导出联系人 /// /summary /// param name="filePath"要导出的路径/param /// returns是否成功导出/returns public string ExportContacts(string filePath) { string isOk = "is not OK"; if (filePath != null) { XmlTextWriter xmlTxtWt = new XmlTextWriter(filePath,Encoding.UTF8); xmlDoc.Save(xmlTxtWt); xmlTxtWt.Close(); isOk = "is OK"; } return isOk; } /// summary /// 导入联系人 /// /summary /// param name="filePath"要导入的路径/param public void ImportContacts(string filePath) { string impFirstName = string.Empty; string impLastName = string.Empty; XmlNode cNode; XmlDocument xmlDoc2 = new XmlDocument(); xmlDoc2.Load(filePath); XmlNodeList contactNodeList = xmlDoc2.SelectNodes("//Contact"); foreach(XmlNode contactNode in contactNodeList) { impFirstName = contactNode.FirstChild.FirstChild.ChildNodes[0].Value; impLastName = contactNode.FirstChild.LastChild.ChildNodes[0].Value; cNode = this.xmlDoc.SelectSingleNode(string.Format("//name[first='{0}' and last='{1}']",impFirstName,impLastName)); if (cNode == null) { XmlNode importNode = xmlDoc.ImportNode(contactNode,true); xmlDoc.SelectSingleNode("/ContactDetails").AppendChild(importNode); } } xmlDoc.Save(this.xmlPath); } #endregion }}