刚到新单位,学习他们的源代码,代码里读写系统配置文件的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文件的基类实现的更多相关文章

  1. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

  2. JAVA_读取XML文件

    在项目中,很多种情况都需要将一些配置写在xml文件或者properties文件里,便于日后修改配置,好维护等等. 1.新建xml文件 <?xml version="1.0" ...

  3. 浅谈JS中的!=、== 、!==、===的用法和区别 JS中Null与Undefined的区别 读取XML文件 获取路径的方式 C#中Cookie,Session,Application的用法与区别? c#反射 抽象工厂

    浅谈JS中的!=.== .!==.===的用法和区别   var num = 1;     var str = '1';     var test = 1;     test == num  //tr ...

  4. Spring相关:jdom学习:读取xml文件

    云课堂马士兵的spring2.5课程中提到的 用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类 ...

  5. 在C#中创建和读取XML文件

    1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...

  6. XML文件与实体类的互相转换

    XML文件与实体类的互相转换 一.将XML文件反序列化为实体类对象 1. 通常程序的配置信息都保存在程序或者网站的专门的配置文件中(App.config/web.config).但是现在为了演示XML ...

  7. Java获取路径方法&相对路径读取xml文件方法

    (1).request.getRealPath("/");//不推荐使用获取工程的根路径 (2).request.getRealPath(request.getRequestURI ...

  8. java 读取XML文件作为配置文件

    首先,贴上自己的实例: XML文件:NewFile.xml(该文件与src目录同级) <?xml version="1.0" encoding="UTF-8&quo ...

  9. jdom学习读取XML文件

    用JDOM读取XML文件需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类.Element类等的方法读取所需的内容.IB ...

随机推荐

  1. MySQL主键设计

    [TOC] 在项目过程中遇到一个看似极为基础的问题,但是在深入思考后还是引出了不少问题,觉得有必要把这一学习过程进行记录. MySQL主键设计原则 MySQL主键应当是对用户没有意义的. MySQL主 ...

  2. Linux网络编程系列-TCP传输控制

    滑动窗口(sliding window) 滑动窗口是用于流量控制的,发送端根据接收端的处理能力发送数据,不至于造成过多的丢包. 是发送方和接收方间的协调,对方的接收窗口大小就是自己的发送窗口大小. 在 ...

  3. 缓存篇(Cache)~第一回 使用static静态成员实现服务器端缓存(导航面包屑)

    返回目录 今天写缓存篇的第一篇文章,在写完目录后,得到了一些朋友的关注,这给我之后的写作带来了无穷的力量,在这里,感谢那几位伙伴,哈哈! 书归正传,今天我带来一个Static静态成员的缓存,其实它也不 ...

  4. fir.im Weekly - 可能是 iOS 审核最全面的解决方案

    ipv6 被拒绝,后台定位被拒绝--让很多国内 iOS 开发者心力交瘁.这是一份关于 iOS 审核的终极免费方案,作者iOSWang对最近iOS 审核被拒问题给出了比较全面的方案:Solve-App- ...

  5. C#、.Net代码精简优化(空操作符(??)、as、string.IsNullOrEmpty() 、 string.IsNullOrWhiteSpace()、string.Equals()、System.IO.Path 的用法)

    一.空操作符(??)在程序中经常会遇到对字符串或是对象判断null的操作,如果为null则给空值或是一个指定的值.通常我们会这样来处理: .string name = value; if (name ...

  6. 每天一个linux命令(43):killall命令

    Linux系统中的killall命令用于杀死指定名字的进程(kill processes by name).我们可以使用kill命令杀死指定进程PID的进程,如果要找到我们需要杀死的进程,我们还需要在 ...

  7. winform 程序制作自己的数字签名(续)

    在上一篇文章<winform 程序制作自己的数字签名>中我们已经可以得到我们程序定制的数字签名了,但是比较讨厌的是每次编译之后,数字签名需要重新手动添加. 我们需要的是在程序编译时自动添加 ...

  8. OpenCascade Primitives BRep - Box

    OpenCascade Primitives BRep - Box eryar@163.com Abstract. BRep is short for Boundary Representation. ...

  9. poj1330Nearest Common Ancestors 1470 Closest Common Ancestors(LCA算法)

    LCA思想:http://www.cnblogs.com/hujunzheng/p/3945885.html 在求解最近公共祖先为问题上,用到的是Tarjan的思想,从根结点开始形成一棵深搜树,非常好 ...

  10. 一步一步学Python(1) 基本逻辑控制举例和编码风格规范

    (1) 基本逻辑控制举例和编码风格规范 1.while死循环 2.for循环 3.if,elif,else分支判断 4.编码风格(官方建议) 版本:Python3.4 1.while死循环 #func ...