C#读取XML文件的基类实现
刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码比较老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很庞大,同时,操作XML的读写操作都是使用SetAttribute和node.Attribute(name)方法,因此,想到结合之前所做的XML操作,完成了一个能够读取XML文件的基类,便于以后的使用。
PS:即使再老套的代码,目前也不敢进行优化,一是水平不行,二是不敢。
使用静态扩展类,扩展了几个经常使用的类型,能够方便数据的读写。
操作XML的类,可以直接继承BaseLinqXmlFileInfo,只需要设置protected类型的变量mPathName,mFileName,然后重写抽象方法即可实现XML文档的读取操作。
PS:能力有限,有不对之处请指正,谢谢。
using System;
using System.IO;
using System.Xml.Linq; namespace Common
{
public abstract class BaseLinqXmlFileInfo
{
protected string mPathName; protected string mFileName; protected virtual string PathName
{
get { return mPathName; }
set { mPathName = value; }
} protected virtual string FileName
{
get { return mFileName; }
set { mFileName = value; }
} protected virtual string FilePathName
{
get
{
return Path.Combine(PathName, FileName);
}
} public virtual bool Load()
{
bool result = false;
try
{
string filePathName = this.FilePathName;
if (File.Exists(filePathName))
{
this.LoadDocument(filePathName);
result = true;
}
}
catch(Exception ex)
{
//异常信息输出
}
return result;
} public virtual bool Save()
{
bool result = false;
try
{ string pathName = this.PathName;
if (!Directory.Exists(pathName))
{
Directory.CreateDirectory(PathName);
}
string tempFileName = "~" + FileName;
string tempfilePathName = Path.Combine(pathName, tempFileName);
this.SaveDocument(tempfilePathName);
string filePathName = Path.Combine(PathName, FileName);
if (File.Exists(filePathName))
{
FileAttributes att = File.GetAttributes(filePathName);
if((att & FileAttributes.ReadOnly)== FileAttributes.ReadOnly)
{
File.SetAttributes(filePathName, att & ~FileAttributes.ReadOnly);
}
}
File.Copy(tempfilePathName, filePathName, true);
if (File.Exists(tempfilePathName))
{
File.Delete(tempfilePathName);
}
result = true;
}
catch (Exception ex)
{
//异常信息输出
}
return result;
} private void LoadDocument(string fileName)
{
try
{
XDocument doc = XDocument.Load(fileName);
this.ReadXml(doc);
}
catch(Exception ex)
{
//异常信息输出
}
} private void SaveDocument(string fileName)
{
try
{
XDocument doc = new XDocument();
this.WriteXml(doc);
doc.Save(fileName);
}
catch(Exception ex)
{
//异常信息输出
}
} private void ReadXml(XDocument doc)
{
XElement root = doc.Root;
this.ReadXml(root);
} private void WriteXml(XDocument doc)
{
XElement root = new XElement("root");
doc.Add(root);
this.WriteXml(root);
} protected abstract void ReadXml(XElement node); protected abstract void WriteXml(XElement node);
} public static class XMLSerializeExtensionClass
{
public static void Write(this XElement node,string name,string value)
{
node.SetAttributeValue(name, value);
} public static string ReadString(this XElement node, string name, string defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? att.Value : defaultValue;
} public static void Write(this XElement node,string name,long value)
{
node.SetAttributeValue(name, value);
} public static long ReadLong(this XElement node,string name,long defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? Convert.ToInt64(att.Value) : defaultValue;
} public static void Write(this XElement node,string name,decimal value)
{
node.SetAttributeValue(name, value);
} public static decimal ReadDecimal(this XElement node,string name,decimal defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? Convert.ToDecimal(att.Value) : defaultValue;
} public static void Write(this XElement node ,string name,DateTime value)
{
node.SetAttributeValue(name, value);
} public static DateTime ReadDateTime(this XElement node,string name,DateTime defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? Convert.ToDateTime(att.Value) : defaultValue;
} public static void Write(this XElement node,string name,int value)
{
node.SetAttributeValue(name, value);
} public static int ReadInt(this XElement node,string name,int defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? Convert.ToInt32(att.Value) : defaultValue;
} public static void Write(this XElement node, string name,bool value)
{
node.SetAttributeValue(name, value);
} public static bool ReadBoolean(this XElement node, string name, bool defaultValue)
{
XAttribute att = node.Attribute(name);
return att != null ? Convert.ToBoolean(att.Value) : defaultValue;
}
}
}
C#读取XML文件的基类实现的更多相关文章
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
- JAVA_读取XML文件
在项目中,很多种情况都需要将一些配置写在xml文件或者properties文件里,便于日后修改配置,好维护等等. 1.新建xml文件 <?xml version="1.0" ...
- 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂
浅谈JS中的!=.== .!==.===的用法和区别 var num = 1; var str = '1'; var test = 1; test == num //tr ...
- Spring相关:jdom学习:读取xml文件
云课堂马士兵的spring2.5课程中提到的 用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类 ...
- 在C#中创建和读取XML文件
1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...
- XML文件与实体类的互相转换
XML文件与实体类的互相转换 一.将XML文件反序列化为实体类对象 1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config).但是现在为了演示XML ...
- Java获取路径方法&相对路径读取xml文件方法
(1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...
- java 读取XML文件作为配置文件
首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...
- jdom学习读取XML文件
用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...
随机推荐
- [源码]NumberToUpper 数字转中文
使用时需开启unsafe选项 构造函数有4个参数 number : 数字文本 isSimplified : 是否只使用简体中文,默认:false isMoney : 是否是金额模式(忽略小数点后3位, ...
- Jetty 9 源码分析 Connector及Server类(一)
本文的源码基于Jetty9,主要分析了Jetty 的Connector与Server类间在Jetty启动过程中的一些细节.Jetty9 对以前的Connector体系进行了重构, 结构与6和7都不同, ...
- Springlake-01 介绍&功能&安装
1. 简介与功能 1)Springlake 是一个企业内容平台SECP 2)是一个可配置的系统,80%内容可以配置 3)允许建立和配置垂直解决方案 4)敏捷和占用空间小,可伸缩 5)端到端的安全性与性 ...
- 创建Cookie,简单模拟登录,记录登录名,购物车记录先前添加内容,session控制登录
工作任务:模拟淘宝登录和购物车功能:使用cookie记录登录名,下次登录时能够记得上次的登录名,使用cookie模拟购物车功能,使用session记住登录信息并验证是否登录,防止利用url打开网站, ...
- java 线程协作 wait(等待)与 notiy(通知)
一.wait().notify()和notifyAll() 为了更好的支持多线程之间的协作,JDK提供了三个重要的本地方法 //调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对 ...
- JS util
一.返回上一页(history) 发觉有两种用法: 1.javascript:history.back(-1); 2.javascript:history.go(-1); 它们俩的区别是: histo ...
- Struts2学习笔记 - HelloWorld总结
相信网上已经有很多关于struts2的HelloWorld可参考,我这里就不重复了,没个学struts2的人都会做过很多个HelloWorld,而我这里就总结一下一个简单的HelloWorld 我在一 ...
- 查看Query Plan
在执行一个查询语句时,查询优化器编译查询语句,产生一个足够好的Compiled Plan,将其缓存到plan cache中.Compiled plan是基于batch的,如果一个batch含有多个qu ...
- php技术之路
按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...
- c#连接mysql环境配置
写.net的时候一直用的都是sql sever,mysql小 有命令行方便就想试了一下,网上搜很久,下载很多配置文件都不成功.昨晚上搞到两点多,冒着生命危险. 后来终于在一个网站上找到这个 MySQL ...