毛毛的小窝 — 关注技术交流、让我们一起成长

导航

C# xml dom应用

编写此案例的目的是为了描述在普通的应用程序中如何运用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; 



Contact 构造器 



Contact 资源释放方法 



Contact 属性 



Contact 功能函数 

}
 

}
 

程序主窗体代码如下: 

namespace ContactApplication 



using System; 

using System.Drawing; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 

using System.Data; 



/// <summary> 

/// Form1 的摘要说明。 

/// </summary> 


public class Form1 : System.Windows.Forms.Form 



private System.Windows.Forms.Label label1; 

private System.Windows.Forms.TextBox textBox1; 

private System.Windows.Forms.RadioButton radioButton1; 

private System.Windows.Forms.RadioButton radioButton2; 

private System.Windows.Forms.Button button1; 

private System.Windows.Forms.Label label2; 

private System.Windows.Forms.ListBox listBox1; 

private System.Windows.Forms.TextBox textBox2; 

private System.Windows.Forms.GroupBox groupBox1; 

private System.Windows.Forms.Button button2; 

private System.Windows.Forms.Button button3; 

private System.Windows.Forms.Label label3; 

private System.Windows.Forms.TextBox textBox3; 

private System.Windows.Forms.Button button4; 

private System.Windows.Forms.Button button5; 

private System.Windows.Forms.Button button6; 

/// <summary> 

/// 必需的设计器变量。 

/// </summary> 


private System.ComponentModel.Container components = null



public Form1() 



// 

// Windows 窗体设计器支持所必需的 

// 

InitializeComponent(); 



// 

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 

// 

}
 



/// <summary> 

/// 清理所有正在使用的资源。 

/// </summary> 


protected override void Dispose( bool disposing ) 



if( disposing ) 



if (components != null



components.Dispose(); 

}
 

}
 

base.Dispose( disposing ); 

}
 



Windows 窗体设计器生成的代码 



/// <summary> 

/// 应用程序的主入口点。 

/// </summary> 


[STAThread] 

static void Main() 



Application.Run(
new Form1()); 

}
 



private void Form1_Load(object sender, System.EventArgs e) 



Contact contact 
= new Contact(); 

this.BindToListBox(contact); 

}
 



private void BindToListBox(Contact contact) 



this.listBox1.Items.Clear(); 

ArrayList nameList; 



try 



nameList 
= contact.LoadContactName(); 

foreach(object obj in nameList) 



this.listBox1.Items.Add(obj.ToString()); 

}
 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

finally 



contact.Dispose(); 

}
 

}
 



private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) 



Contact contact 
= new Contact(); 

string itemText = this.listBox1.GetItemText(listBox1.SelectedItem); 

string firstName = itemText.Split(" ".ToCharArray())[1].Trim().ToString(); 

string lastName = itemText.Split(" ".ToCharArray())[0].Trim().ToString(); 



contact.FirstName 
= firstName; 

contact.LastName 
= lastName; 

try 



this.textBox2.Text = contact.DisplayNote(); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

finally 



contact.Dispose(); 

}
 

}
 



private void button1_Click(object sender, System.EventArgs e) 



string searchType = string.Empty; 

string searchValue = string.Empty; 

Contact contact 
= new Contact(); 



if (this.radioButton1.Checked == true

searchType 
= "first"

if (this.radioButton2.Checked == true

searchType 
= "last"

if (this.textBox1.Text != ""

searchValue 
= this.textBox1.Text; 



try 



contact.SearchContact(searchType,searchValue); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

finally 



contact.Dispose(); 

}
 

}
 



private void button4_Click(object sender, System.EventArgs e) 



string firstName = string.Empty; 

string lastName = string.Empty; 

string note = string.Empty; 



Form2 frm2 
= new Form2(); 

frm2.ShowDialog(); 



if (frm2.DialogResult == DialogResult.OK) 



firstName 
= frm2.Controls[0].Text; 

lastName 
= frm2.Controls[1].Text; 

note 
= frm2.Controls[2].Text; 



Contact contact 
= new Contact(firstName,lastName,note); 

try 



contact.AddContact(); 

this.BindToListBox(contact); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

}
 

}
 



private void button5_Click(object sender, System.EventArgs e) 



if (this.listBox1.SelectedItem == null



MessageBox.Show(
"要修改联系人信息请先在联系人列表中选中","修改联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk); 

return

}
 

string firstName = string.Empty; 

string lastName = string.Empty; 

string note = string.Empty; 

string itemText = string.Empty; 



itemText 
= this.listBox1.GetItemText(listBox1.SelectedItem); 

firstName 
= itemText.Split(" ".ToCharArray())[1].Trim().ToString(); 

lastName 
= itemText.Split(" ".ToCharArray())[0].Trim().ToString(); 



Contact contact 
= new Contact(); 

contact.FirstName 
= firstName; 

contact.LastName 
= lastName; 

try 



note 
= contact.DisplayNote(); 

contact.SelectContact(); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 



Form3 frm3 
= new Form3(); 

frm3.Controls[
0].Text = firstName; 

frm3.Controls[
1].Text = lastName; 

frm3.Controls[
2].Text = note; 

frm3.ShowDialog(); 



if (frm3.DialogResult == DialogResult.OK) 



firstName 
= frm3.Controls[0].Text; 

lastName 
= frm3.Controls[1].Text; 

note 
= frm3.Controls[2].Text; 



contact.FirstName 
= firstName; 

contact.LastName 
= lastName; 

contact.Note 
= note; 

try 



contact.UpdateContact(); 

this.BindToListBox(contact); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

}
 

}
 



private void button6_Click(object sender, System.EventArgs e) 



if (this.listBox1.SelectedItem == null



MessageBox.Show(
"要删除联系人信息请先在联系人列表中选中","删除联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk); 

return

}
 

string firstName = string.Empty; 

string lastName = string.Empty; 

string itemText = string.Empty; 



itemText 
= this.listBox1.GetItemText(listBox1.SelectedItem); 

firstName 
= itemText.Split(" ".ToCharArray())[1].Trim().ToString(); 

lastName 
= itemText.Split(" ".ToCharArray())[0].Trim().ToString(); 



if (MessageBox.Show("是否确定删除联系人,删除后将不可恢复!","删除联系人",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK) 



Contact contact 
= new Contact(); 

contact.FirstName 
= firstName; 

contact.LastName 
= lastName; 

try 



contact.SelectContact(); 

contact.DeleteContact(); 

this.BindToListBox(contact); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

}
 

}
 



private void button3_Click(object sender, System.EventArgs e) 



string filePath = this.textBox3.Text; 



Contact contact 
= new Contact(); 

try 



MessageBox.Show(
"Export " + contact.ExportContacts(filePath),"导出联系人",MessageBoxButtons.OK,MessageBoxIcon.Asterisk); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

finally 



contact.Dispose(); 

}
 

}
 



private void button2_Click(object sender, System.EventArgs e) 



if (this.textBox3.Text == ""

return



string filePath = this.textBox3.Text; 

Contact contact 
= new Contact(); 

try 



contact.ImportContacts(filePath); 

this.BindToListBox(contact); 

}
 

catch(Exception exp) 



MessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 

}
 

}
 

}
 

}
 

添加联系人窗体代码如下: 

namespace ContactApplication 



using System; 

using System.Drawing; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 



/// <summary> 

/// Form2 的摘要说明。 

/// </summary> 


public class Form2 : System.Windows.Forms.Form 



private System.Windows.Forms.Button button1; 

private System.Windows.Forms.Button button2; 

private System.Windows.Forms.TextBox textBox1; 

private System.Windows.Forms.TextBox textBox2; 

private System.Windows.Forms.TextBox textBox3; 

private System.Windows.Forms.Label label1; 

private System.Windows.Forms.Label label2; 

private System.Windows.Forms.Label label3; 

private System.Windows.Forms.Label label4; 

/// <summary> 

/// 必需的设计器变量。 

/// </summary> 


private System.ComponentModel.Container components = null



public Form2() 



// 

// Windows 窗体设计器支持所必需的 

// 

InitializeComponent(); 



// 

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 

// 

}
 



/// <summary> 

/// 清理所有正在使用的资源。 

/// </summary> 


protected override void Dispose( bool disposing ) 



if( disposing ) 



if(components != null



components.Dispose(); 

}
 

}
 

base.Dispose( disposing ); 

}
 



Windows 窗体设计器生成的代码 



private void Form2_Load(object sender, System.EventArgs e) 





}
 

}
 

}
 

修改联系人窗体如下: 

namespace ContactApplication 



using System; 

using System.Drawing; 

using System.Collections; 

using System.ComponentModel; 

using System.Windows.Forms; 



/// <summary> 

/// Form3 的摘要说明。 

/// </summary> 


public class Form3 : System.Windows.Forms.Form 



private System.Windows.Forms.Label label1; 

private System.Windows.Forms.Label label2; 

private System.Windows.Forms.Label label3; 

private System.Windows.Forms.Label label4; 

private System.Windows.Forms.TextBox textBox1; 

private System.Windows.Forms.TextBox textBox2; 

private System.Windows.Forms.TextBox textBox3; 

private System.Windows.Forms.Button button1; 

private System.Windows.Forms.Button button2; 

/// <summary> 

/// 必需的设计器变量。 

/// </summary> 


private System.ComponentModel.Container components = null



public Form3() 



// 

// Windows 窗体设计器支持所必需的 

// 

InitializeComponent(); 



// 

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 

// 

}
 



/// <summary> 

/// 清理所有正在使用的资源。 

/// </summary> 


protected override void Dispose( bool disposing ) 



if( disposing ) 



if(components != null



components.Dispose(); 

}
 

}
 

base.Dispose( disposing ); 

}
 



Windows 窗体设计器生成的代码 



private void Form3_Load(object sender, System.EventArgs e) 





}
 

}
 

}
 

posted on 2007-05-31 18:10  mjgforever  阅读(448)  评论(0编辑  收藏  举报