C#操作XML初步(8)
第四章:通用的XML处理方法(2)
上一篇,我们提供了一个类,用来操作XML文件,这里提供一个应用的例子,来源于无聊时候写的一个XML留言本中对留言数据的处理
代码如下: using System;
using System.IO;
using System.Data;
using System.Diagnostics;
using XmlBook.Com.Sem.Tools; //这里就包含了上一篇提供的类
namespace XmlBook.Component
{
/// <summary>
/// 版权: Copyright by SEM IT Department
/// 版本: 0.0.1
/// 文件: XmlBook.Component.MdlDataMange.cs
/// 目的: 留言数据相关的操作
/// 作者: 欧远宁 @2005-04-09
/// 邮箱: outrace@soueast-motor.com
/// 修改:
/// </summary>
public class MdlDataManage
{
#region 私有成员
private string strID = null;
private string strName = null;
private string strSex = null;
private string strEmail = "no email";
private string strPage = "no page";
private string strQQ = "no QQ";
private string strSubject = null;
private string strContent = null;
private string strReply = "no reply";
private string strFilter = null;
#endregion
#region 公有成员
/// <summary>
/// 姓名
/// </summary>
public string StrName
{
get{return this.strName;}
set{this.strName = value;}
}
/// <summary>
/// 性别
/// </summary>
public string StrSex
{
get{return this.strSex;}
set{this.strSex = value;}
}
/// <summary>
/// 邮箱
/// </summary>
public string StrEmail
{
get{return this.strEmail;}
set{this.strEmail = value;}
}
/// <summary>
/// 主页
/// </summary>
public string StrPage
{
get{return this.strPage;}
set{this.strPage = value;}
}
/// <summary>
/// OICQ号码
/// </summary>
public string StrQQ
{
get{return this.strQQ;}
set{this.strQQ = value;}
}
/// <summary>
/// 主题
/// </summary>
public string StrSubject
{
get{return this.strSubject;}
set{this.strSubject = value;}
}
/// <summary>
/// 内容
/// </summary>
public string StrContent
{
get{return this.strContent;}
set{this.strContent = value;}
}
/// <summary>
/// 回复
/// </summary>
public string StrReply
{
get{return this.strReply;}
set{this.strReply = value;}
}
/// <summary>
/// ID号
/// </summary>
public string StrID
{
get{return this.strID;}
set{this.strID = value;}
}
public string StrFilter
{
get{return this.strFilter;}
set{this.strFilter = value;}
}
#endregion
public MdlDataManage()
{
//
// TODO: 留言数据相关的操作
//
}
/// <summary>
/// 新增一条留言(这里演示如何新增一笔记录)
/// </summary>
public void Add()
{
string [] arrFileds = new string[10];
string [] arrValues = new string[10];
arrFileds[0] = "ID";
arrFileds[1] = "Name";
arrFileds[2] = "Sex";
arrFileds[3] = "Email";
arrFileds[4] = "Page";
arrFileds[5] = "QQ";
arrFileds[6] = "Subject";
arrFileds[7] = "Content";
arrFileds[8] = "Reply";
arrFileds[9] = "DateTime";
arrValues[0] = DateTime.Now.ToFileTime().ToString();
arrValues[1] = this.strName;
arrValues[2] = this.strSex;
arrValues[3] = this.strEmail;
arrValues[4] = this.strPage;
arrValues[5] = this.strQQ;
arrValues[6] = this.strSubject;
arrValues[7] = this.strContent;
arrValues[8] = "no reply";
arrValues[9] = DateTime.Now.ToString();
try
{
XmlDataBase xml = new XmlDataBase();
xml.StrDataFile = Config.DataFile;
xml.StrFields = arrFileds;
xml.StrData = arrValues;
xml.Insert();
xml.Clear();
}
catch(Exception ee)
{
Log log = new Log();
log.StrUser = "系统";
log.StrDepartment = "签写留言";
log.StrFileName = "XmlBook.Component.MdlDataManage.cs";
log.StrDescription = ee.Message;
log.WriteLog();
}
}
/// <summary>
/// 回复留言(这里演示如何修改某一笔XML记录)
/// </summary>
public void Reply()
{
string [] arrFileds = new string[1];
string [] arrValues = new string[1];
arrFileds[0] = "Reply";
arrValues[0] = this.strReply;
try
{
XmlDataBase xml = new XmlDataBase();
xml.StrDataFile = Config.DataFile;
xml.StrData = arrValues;
xml.StrFields = arrFileds;
xml.StrFilter = "ID='"+this.StrID+"'";
xml.Update();
xml.Clear();
}
catch(Exception ee)
{
Log log = new Log();
log.StrUser = "系统";
log.StrDepartment = "回复数据";
log.StrFileName = "XmlBook.Component.MdlDataManage.cs";
log.StrDescription = ee.Message;
log.WriteLog();
}
}
/// <summary>
/// 删除留言(这里演示如何删除一笔记录)
/// </summary>
public void Delete()
{
try
{
XmlDataBase xml = new XmlDataBase();
xml.StrDataFile = Config.DataFile;
xml.StrFilter = "ID = '"+ this.strID +"'";
xml.Delete();
xml.Clear();
}
catch(Exception ee)
{
Log log = new Log();
log.StrUser = "系统";
log.StrDepartment = "回复数据";
log.StrFileName = "XmlBook.Component.MdlDataManage.cs";
log.StrDescription = ee.Message;
log.WriteLog();
}
}
/// <summary>
/// 获取几行行留言数据(这里演示获取一行或多行数据)
/// </summary>
/// <returns>各行数据</returns>
public DataRow[] GetOneRow()
{
XmlDataBase xml = new XmlDataBase();
DataRow[] dr = null;
try
{
xml.StrDataFile = Config.DataFile;
xml.StrFilter = this.strFilter;
dr = xml.SelectRows();
return dr;
}
catch(Exception ee)
{
Log log = new Log();
log.StrUser = "系统";
log.StrDepartment = "获取一行数据";
log.StrFileName = "XmlBook.Component.MdlDataManage.cs";
log.StrDescription = ee.Message;
log.WriteLog();
return dr;
}
finally
{
xml.Clear();
}
}
/// <summary>
/// 获取视图
/// </summary>
/// <returns>视图</returns>
public DataView GetView()
{
XmlDataBase xml = new XmlDataBase();
DataView dv = null;
try
{
xml.StrDataFile = Config.DataFile;
xml.StrFilter = this.strFilter;
dv = xml.SelectView();
return dv;
}
catch(Exception ee)
{
Log log = new Log();
log.StrUser = "系统";
log.StrDepartment = "获取视图";
log.StrFileName = "XmlBook.Component.MdlDataManage.cs";
log.StrDescription = ee.Message;
log.WriteLog();
return dv;
}
finally
{
xml.Clear();
}
}
}
}
希望通过上面的例子能给大家操作XML文件提供一点点的借鉴。对于XML文件操作的连载就先到这里。如果大家觉得对你有些帮助。请给我鼓下掌。
我也是刚学习的菜鸟,很多想法、做法都很幼稚,高手不要取笑哦。
文中提到的我写的一个XML留言本,如果谁有需要原代码的,请跟我联系,我没有空间可以上传,大家可以给我发邮件:outrace@soueast-motor.com。多交流