C#读取XML文件的基类实现

时间:2023-03-09 03:22:03
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;
}
}
}