刚到新单位,学习他们的源代码,代码里读写系统配置文件的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. MooseFS学习-概述

    MFS(MooseFS)是一个容错的.网络分布式文件系统,是GFS的开源实现.它把数据分散在多个物理机上,对外展现为一个整体资源. 支持的功能 Unix的通用文件系统功能:目录树:记录POSIX文件属 ...

  2. MyBatis入门学习(一)

    一.MyBatis入门简要介绍(百科) MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyB ...

  3. NSDate NSString相互转化

    时间戳是经常用到的,今天就总结一下 //设置转化格式 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter s ...

  4. 分割超大Redis数据库例子

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/124.html?1455853509 薄荷 App 上的伙伴功能大量使用了 ...

  5. 初入职场的建议--摘自GameRes

    又开始一年一度的校招了,最近跑了几个学校演讲,发现很多话用短短的一堂职业规划课讲还远远不够,因为那堂课仅仅可能帮大家多思考怎样找到一份合适的工作,并没有提醒大家怎样在工作中发展自己的职业. 见过这么多 ...

  6. 函数柯理化以及利用柯理化实现bind方法

    1.函数柯理化 把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术. 柯理化函数思想:一个js预先处理的思想:利用函数执行可以形 ...

  7. Atitti 知识图谱构建方法attilax 总结

    Atitti 知识图谱构建方法attilax 总结   1.1. 知识图谱schema构建(体系化)1 1.2. 纵向垂直拓展(向上抽象,向下属性拓展)2 1.3. 横向拓展2 1.4. 网拓展2 1 ...

  8. fir.im Weekly - iOS开发中的Git流程

    本期 fir.im Weekly 收集了微博上的热转资源,包含 Android.iOS 开发工具.源码等好用的轮子,还有一些 APP 设计的 Tips,希望对你有用. 精仿知乎日报 iOS 端 @我偏 ...

  9. EFProf Entity Framework Profile 工具

    SQL Server Profiler用来跟踪应用程序发送到SQL Server中的SQL语句,用于检测性能,查找问题.Entity Framework 也有它的跟踪工具EFProf,用于跟踪Enti ...

  10. poj 3352Road Construction(无向双连通分量的分解)

    /* 题意:给定一个连通的无向图G,至少要添加几条边,才能使其变为强连通图(指的是边强联通). 思路:利用tarjan算法找出所有的双联通分量!然后根据low[]值的不同将双联通分量 进行缩点,最后图 ...