using system; using system.text; using system.io; using system.xml; using system.data;
namespace xmlbook.com.sem.tools { /// <summary> /// 版权: copyright by sem it department /// 版本: 0.0.1 /// 文件: xmlbook.com.sem.tools.xmldatabase.cs /// 目的: 提供将xml当作数据库处理的一些方法 /// 作者: 欧阳云天 @2005-04-09 /// 邮箱: outrace@soueast-motor.com /// 修改: /// </summary> public class xmldatabase { #region 私有成员
private string strdatafile = null; /// <summary> /// 数据集 /// </summary> private dataset myds = null; /// <summary> /// 字符过滤数组 比如 “id=1 and username=trace” /// </summary> private string strfilter = null; /// <summary> /// 排序的字段 比如 “id desc,username” /// </summary> private string strsort = null; /// <summary> /// 数据集合中的字段名集合 /// </summary> private string[] strfields = null; /// <summary> /// 数据集合中的数据数组 /// </summary> private string[] strdata = null; /// <summary> /// 模板文件的全路径 /// </summary> private string strtemplatefile = null; #endregion
#region 公共属性 /// <summary> /// 模板文件路径 /// </summary> public string strtemplatefile { set{this.strtemplatefile = value;} get{return this.strtemplatefile;} } /// <summary> /// 数据文件路径 /// </summary> public string strdatafile { set{this.strdatafile = value;} get{return this.strdatafile;} } /// <summary> /// 字符过滤数组 /// </summary> public string strfilter { set{this.strfilter = value;} } /// <summary> /// 排序的字段 /// </summary> public string strsort { set{this.strsort = value;} } /// <summary> /// 数据集合中的字段名 /// </summary> public string[] strfields { set{this.strfields = value;} } /// <summary> /// 数据集合中的数据数组 /// </summary> public string[] strdata { set{this.strdata = value;} } /// <summary> /// 数据集合,可以放在缓存供调用 /// </summary> public dataset myds { set{this.myds = value;} get{return this.myds;} } #endregion
public xmldatabase() { // // todo: 提供将xml当作数据库处理的一些方法 // } /// <summary> /// 取得xml文件的内容并填入dataset /// </summary> private void open() { try { this.myds = new dataset(); filestream fin ; fin = new filestream(this.strdatafile, filemode.open, fileaccess.read, fileshare.readwrite); this.myds.readxml(fin); fin.close(); } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “读取xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); } }
/// <summary> /// 将操作结果写入xml /// </summary> private void save() { try { this.myds.writexml(this.strdatafile, xmlwritemode.writeschema); } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “保存xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); } }
/// <summary> /// 取得特定的数据视图 /// 一般在数据绑定的时候,我们可以很方便的生成供绑定的视图 /// </summary> /// <returns>数据视图</returns> public dataview selectview() { if(this.myds == null) this.open();
dataview mydv = new dataview(this.myds.tables[0]);
if (strfilter != null) mydv.rowfilter = this.strfilter;
mydv.sort = this.strsort;
return mydv; }
/// <summary> /// 取得特定的行 /// 使用行的方式是因为有些时候,我们仅仅只需要某一行或多行记录 /// 比如我们判断登陆的时候,只是需要某个id的所在行,然后匹配它的密码项 /// </summary> /// <returns>各行数据</returns> public datarow[] selectrows() { if(this.myds == null) this.open(); datarow[] myrows = myds.tables[0].select(this.strfilter); return myrows; }
/// <summary> /// 往xml当中插入一条数据 /// </summary> /// <returns>操作是否成功</returns> public bool insert() { if(this.myds == null) this.open();
try { datarow newrow = myds.tables[0].newrow();
for (int i = 0; i < this.strfields.length; i++) { newrow[this.strfields[i]] = this.strdata[i]; }
myds.tables[0].rows.add(newrow); this.save(); return true; } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “写入xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); return false; } }
/// <summary> /// 更新数据,这个时候要确保strfields 与 strdata 两个数组的维数一致 /// </summary> /// <returns>是否更新成功</returns> public bool update() { if(this.myds == null) this.open();
try { datarow[] editrow = myds.tables[0].select(this.strfilter);
for(int j=0; j< editrow.length; j++) { for (int i = 0; i < this.strfields.length; i++) { editrow[j][this.strfields[i]] = this.strdata[i]; } } this.save(); return true; } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “更新xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); return false; } }
/// <summary> /// 删除数据 /// </summary> /// <returns>是否删除成功</returns> public bool delete() { if(this.myds == null) this.open();
try { datarow[] editrow = myds.tables[0].select(this.strfilter); for(int i=0;i<editrow.length;i++) { editrow[i].delete(); } this.save(); return true; } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “删除xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); return false; } }
/// <summary> /// 根据一个模板,新建一个xml文件(前提是必须有一个模板文件、并确定目标文件路径) /// </summary> /// <returns>写入是否成功</returns> public bool create() { try { xmldocument doc = new xmldocument(); xmltextreader reader = new xmltextreader(this.strtemplatefile); doc.load(reader); xmlelement member; xmlnode root = doc.documentelement;
for (int i = 0; i < this.strfields.length; i++) { member = doc.createelement(strfields[i].tostring()); member.innertext = this.strdata[i].tostring(); root.appendchild(member); }
xmltextwriter xmlwriter = new xmltextwriter(this.strdatafile,null); xmlwriter.formatting = formatting.indented; doc.save(xmlwriter);
xmlwriter.close(); reader.close(); return true; } catch(exception ee) { log log = new log(); log.struser = “系统”; log.strdepartment = “新建xml数据”; log.strfilename = “com.sem.tools.xmldatabase”; log.strdescription = ee.message; log.writelog(); return false; } }
/// <summary> /// 释放资源 /// </summary> public void clear() { if (this.myds != null) { this.myds.dispose(); } } } }
|