知识大全 C#中使用XML---基于DOM的案例分析

Posted 文件

篇首语:春蚕到死丝方尽,人至期颐亦不休。一息尚存须努力,留作青年好范畴。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#中使用XML---基于DOM的案例分析相关的知识,希望对你有一定的参考价值。

C#中使用XML---基于DOM的案例分析  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  编写此案例的目的是为了描述在普通的应用程序中如何运用DOM技术以及对上一篇文章《C#中使用XML——实现DOM》中所讲述的DOM的相关知识回顾一下 本案例将分析一个联系人应用程序 在这里将XML文档充当数据库来使用 所有的联系人信息存储在XML文档中 同时 在程序中使用DOM对联系人文档进行查询 编辑 更新等操作 具体来说本案例将实现以下功能      . 添加一个新的联系人     . 修改现有联系人     . 删除现有联系人     . 按姓氏查询联系人     . 按名字查询联系人     . 将所有联系人导出到另一个XML文件     . 将联系人从另一个XML文件导入    以下是程序运行效果图     应用程序主窗体      添加联系人窗体      修改联系人窗体     以下是用于测试程序的XML文件     contact xml 将该文件保存在项目目录下    <?xml version= encoding= gb ?>    <ContactDetails>    <Contact>    <name>    <first>Steven</first>    <last>Perez</last>    </name>    <note> cn;system at ;/note>    </Contact>    <Contact>    <name>    <first>Billoys</first>    <last>Perez</last>    </name>    <note>;system at </note>    </Contact>    <Contact>    <name>    <first>刘</first>    <last>罗锅</last>    </name>    <note>古代人</note>    </Contact>    </ContactDetails>    contact xml 该文件用于实现导入联系人功能 将该文件随便保存在一个目录下然后将保存路径连同文件名拷贝到主窗体的 保存的路径 文本框中再单击 导入 按纽即可实现导入功能     <?xml version= encoding= gb ?>    <ContactDetails>    <Contact>    <name>    <first>Steven</first>    <last>Perez</last>    </name>    <note> cn;system at ;/note>    </Contact>    <Contact>    <name>    <first>Billoys</first>    <last>Perez</last>    </name>    <note>;system at </note>    </Contact>    <Contact>    <name>    <first>刘</first>    <last>德华</last>    </name>    <note>香港著名艺人 工作勤恳同时不忘生活 出演电影 多部 演技已达登峰造极 刻画人物栩栩如生</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>    /// 个人信息  < cha138/Article/program/net/201311/12660

相关参考

知识大全 DOM、JDOM、DOM4J解析XML实例

DOM、JDOM、DOM4J解析XML实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  <

知识大全 在Visual C#中使用XML之编写XML

在VisualC#中使用XML之编写XML  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!在我的上一

知识大全 C#中XML的写入方法

C#中XML的写入方法  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!    usingSystem

知识大全 Struts中如何基于validation.xml来启用客户端验证

Struts中如何基于validation.xml来启用客户端验证?  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快

知识大全 在 Java 程序设计中处理 DOM 异常

在Java程序设计中处理DOM异常  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在使用基于Ja

知识大全 C#操作XML

C#操作XML  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  已知有一个XML文件(bookst

知识大全 C#来创建和读取XML文档

C#来创建和读取XML文档  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  扩展标记语言XML(e

知识大全 构建基于Web/XML的信息集成研究

构建基于Web/XML的信息集成研究  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  摘要在当前的

知识大全 用Java实现基于XML的购物车

用Java实现基于XML的购物车  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  购物车是电子商务

知识大全 使用c#(datagrid控件)编辑xml文件

  对xml文件的记录进行删除修改或增加新记录   利用了datagrid控件的sortmand事件对xml里的记录进行排序  email:  &nb