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 ...
随机推荐
- 06- Shell脚本学习--其它
Shell输入输出重定向 Unix 命令默认从标准输入设备(stdin)获取输入,将结果输出到标准输出设备(stdout)显示.一般情况下,标准输入设备就是键盘,标准输出设备就是终端,即显示器. 输出 ...
- MyBatis学习总结(一)——MyBatis快速入门
一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...
- Atitit dsl exer v3 qb3 新特性
Atitit dsl exer v3 qb3 新特性 /atiplat_cms/src/com/attilax/dsl/DslParser.java V3 支持typeed参数,与简化的notyp参数 ...
- salesforce 零基础学习(四十一)Group
salesforce中,有的时候我们需要将一组用户放进一个Group,用来实现以下主要功能: 1.通过sharing rule设置默认的共享访问; 2.将记录分享给其他用户; 3.指定同步的联系人,这 ...
- JS util
一.返回上一页(history) 发觉有两种用法: 1.javascript:history.back(-1); 2.javascript:history.go(-1); 它们俩的区别是: histo ...
- vuejs - the component is a fragment instance
vuejs - the component is a fragment instance http://vuejs.org/guide/components.html#Fragment-Instanc ...
- JS checkbox 全选 全不选
/* JS checkbox 全选 全不选 Html中checkbox: <input type="checkbox" name="cbx" value= ...
- hibernate(五)核心开发接口与对象的三种状态
本文链接:http://www.orlion.ml/37/ 一.Configuration 1.AnnotationConfiguration 2.进行配置信息的管理 3.configure()方法通 ...
- 信息加密之信息摘要加密MD2、MD4、MD5
对于用户数据的保密一直是各个互联网企业头疼的事,那如何防止用户的个人信息泄露呢?今天为大家介绍一种最简单的加密方式--信息摘要算法MD.它如何来保护用户的个人信息呢?其实很简单,当获得到用户的信息后, ...
- php易混淆知识点
一.define(“constant”, “hello world”);和const constant = “hello world”;的区别? (0).使用const使得代码简单易读,const本 ...