首页 >> XML/WebService >> 正文
C#中使用XML---基于DOM的案例分析
来源:Dotnet频道 作者:采集 时间:2008-3-31


  编写此案例的目的是为了描述在普通的应用程序中如何运用DOM技术以及对上一篇文章《C#中使用XML——实现DOM》中所讲述的DOM的相关知识回顾一下,本案例将分析一个联系人应用程序,在这里将XML文档充当数据库来使用, 所有的联系人信息存储在XML文档中,同时,在程序中使用DOM对联系人文档进行查询、编辑、更新等操作。具体来说本案例将实现以下功能:
  
  1. 添加一个新的联系人
  
  2. 修改现有联系人
  
  3. 删除现有联系人
  
  4. 按姓氏查询联系人
  
  5. 按名字查询联系人
  
  6. 将所有联系人导出到另一个XML文件
  
  7. 将联系人从另一个XML文件导入
  
  以下是程序运行效果图:
  
  应用程序主窗体:
   
  添加联系人窗体:
   
  修改联系人窗体:
  
  以下是用于测试程序的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>重案六组探员,富有人情味,对扬震早已芳心默许,知道为什么吗?因为她天生就爱保护别人,当她看到扬震被别人用枪指着头吓的回不过神来时就对这个真实的男人产生了感觉,真可谓巾帼不让须眉</note>
  
  </Contact>
  
  </ContactDetails>
  
  导出联系人时在“保存的路径”文本框中输入一个文件路径,程序将在该路径下创建一个XML文件,如果该文件存在于该路径上,程序将对该XML文件进行重写。
  
  为实现以上所述所有功能,我专门编写了一个类来封装实现代码,该类代码如下:
  
  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>
  
  /// 个人信息
  <
相关新闻
相关评论
 
评论表单加载中...
 
XML/WebService文章

 在Visual C++应

 编辑:admin

 时间:2008-3-10


   .NET Framework 中多语言支持的实现-.NET Framework
   将Eiffel系统集成到.NET Framework中-.NET Framework
   04年五种常用RAD的测验比较(1)-.NET Framework
   04年五种常用RAD的测验比较(2)-.NET Framework
   04年五种常用RAD的测验比较(3)-.NET Framework
最新文章
   .NET Framework 中多语言支持的实现-.NET Framework
   将Eiffel系统集成到.NET Framework中-.NET Framework
   04年五种常用RAD的测验比较(1)-.NET Framework
   04年五种常用RAD的测验比较(2)-.NET Framework
   04年五种常用RAD的测验比较(3)-.NET Framework
总站搜索
搜索
 
热门文章
   oracle数据库文件中的导入\导出
   用Oracle10g列值掩码技术隐藏敏感数据
   VB程序中用ADO对象动态创建数据库和表-VB.NET
   用VB6写简单程序 让电骡自动关机-VB.NET
   使用.NET2.0编写COM组件供VB调用-VB.NET
   VB.NET:键盘控制焦点移动-VB.NET
   用VB.NET绘制GDI图形-VB.NET
   vb.net中应用 ArrayList 实例-VB.NET
 
推荐文章
ASP.NET中的状态管理-ASP.NET
VC、IE、ASP环境下打印、预备的完美解决方案
oracle数据库文件中的导入\导出
VB.NET中快速访问注册表技巧-VB.NET
在vb中实现超连接的方法!和直接发邮件-VB.NET
用VB做realplayer播放列表-VB.NET
在VB.NET中如何实现和利用SortedLists-VB.NET
利用VB.NET Stopwatch对象记录时间-VB.NET
成都古羌科技有限公司版权所有: Copyright@2007-2010 ,ALL Rights Reserved 蜀ICP备07017240号