C#操作XML初步(7)
第四章:通用的XML处理方法(1)
既然我们能够使用DATASET来操作XML文件,那真实太方便了,他完全有能力将一个XML文件当作一张表来使用,那又何乐而不为呢?
于是我们可以同过这样的一个C#类来操作XML,完成类似数据库般的操作: 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();
}
}
}
}
类中引入了另外一个LOG处理的类,在这里一并发给大家
using System;
using System.Xml;
using System.Web;
namespace XmlBook.Com.Sem.Tools
{
/// <summary>
/// 版权: Copyright by SEM IT Department
/// 版本: 0.0.1
/// 文件: XmlBook.Com.Sem.Tools.Log.cs
/// 目的: 提供一个写LOG的方法(就是将错误写入XML记录当中)
/// 作者: 欧远宁 @2005-04-09
/// 邮箱: outrace@soueast-motor.com
/// 修改:
/// </summary>
public class Log
{
#region 私有成员
private HttpContext objContext = HttpContext.Current;
/// <summary>
/// 日志文件路径
/// </summary>
private string logFile = null;
/// <summary>
/// 操作人员
/// </summary>
private string strUser = null;
/// <summary>
/// 所属版块
/// </summary>
private string strDepartment = null;
/// <summary>
/// 正在操作的文件名
/// </summary>
private string strFileName = null;
/// <summary>
/// 操作时间
/// </summary>
private string strTime = null;
/// <summary>
/// 错误描述
/// </summary>
private string strDescription = null;
#endregion
#region 公共属性
/// <summary>
/// 操作人员
/// </summary>
public string StrUser
{
get{return this.strUser;}
set{this.strUser = value;}
}
/// <summary>
/// 正在操作的文件名
/// </summary>
public string StrFileName
{
get{return this.strFileName;}
set{this.strFileName = value;}
}
/// <summary>
/// 所属版块
/// </summary>
public string StrDepartment
{
get{return this.strDepartment;}
set{this .strDepartment = value;}
}
/// <summary>
/// 操作时间
/// </summary>
public string StrTime
{
get{return this.strTime;}
set{this.strTime = value;}
}
/// <summary>
/// 错误描述
/// </summary>
public string StrDescription
{
get{return this.strDescription;}
set{this.strDescription = value;}
}
#endregion
public Log()
{
//
// TODO: 提供一个写LOG的方法(就是将错误写入XML记录当中)
//
}
/// <summary>
/// 将内容写入日志文件
/// </summary>
public void WriteLog()
{
this.logFile = this.objContext.Server.MapPath("./Log/log.config");
try
{
XmlDocument doc = new XmlDocument();
doc.Load(this.logFile);
XmlElement newLog = doc.CreateElement("Log");
XmlElement newUser = doc.CreateElement("User");
newUser.InnerText = this.strUser;
newLog.AppendChild(newUser);
XmlElement newDepartment = doc.CreateElement("Department");
newDepartment.InnerText = this.strDepartment;
newLog.AppendChild(newDepartment);
XmlElement newFileName = doc.CreateElement("FileName");
newFileName.InnerText = this.StrFileName;
newLog.AppendChild(newFileName);
XmlElement newTime = doc.CreateElement("Time");
newTime.InnerText = DateTime.Now.ToString();
newLog.AppendChild(newTime);
XmlElement newDescription = doc.CreateElement("Description");
newDescription.InnerText = this.strDescription;
newLog.AppendChild(newDescription);
doc.DocumentElement.AppendChild(newLog);
doc.Save(this.logFile);
}
catch(Exception ex)
{
HttpContext objContext = HttpContext.Current;
objContext.Response.Write(ex.Message);
}
finally
{
}
}
}
}
这样我们就可以非常方便的将一个XML文件当作是一张表来操作了 使用的例子,我将在下一篇给出