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文件进行重写。
[责任编辑:cndownzcom]
为实现以上所述所有功能,我专门编写了一个类来封装实现代码,该类代码如下:
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
在百度中搜索更多VC#中使用XML之基于DOM的案例分析相关网页
转贴于:中国下载站
【中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页】