C#操作XML初步(7) |
第四章:通用的XML处理方法(1) 既然我们能够使用DATASET来操作XML文件,那真实太方便了,他完全有能力将一个XML文件当作一张表来使用,那又何乐而不为呢? 于是我们可以同过这样的一个C#类来操作XML,完成类似数据库般的操作: 类中引入了另外一个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文件当作是一张表来操作了 使用的例子,我将在下一篇给出 |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理