string defaultValue)142{143XAttribute att = node.Attribute(

时间:2022-03-06 02:42:17

刚到新单位,学习他们的源代码,代码里读写系统配置文件的XML代码对照老套,直接写在一个系统配置类里,没有进行类的拆分,造成类很复杂,同时,操纵XML的读写操纵都是使用SetAttribute和node.Attribute(name)要领,因此,想到结合之前所做的XML操纵,完成了一个能够读取XML文件的基类,便于以后的使用。

PS:即使再老套的代码,目前也不敢进行优化,,一是程度不行,二是不敢。

使用静态扩展类,扩展了几个经常使用的类型,能够便利数据的读写。

操纵XML的类,可以直接担任BaseLinqXmlFileInfo,只需要设置protected类型的变量mPathName,mFileName,然后重写抽象要领即可实现XML文档的读取操纵。

PS:能力有限,有不同错误之处请斧正,感谢。

1 using System; 2 using System.IO; 3 using System.Xml.Linq; 4 5 namespace Common 6 { 7 public abstract class BaseLinqXmlFileInfo 8 { 9 protected string mPathName; 10 11 protected string mFileName; 12 13 protected virtual string PathName 14 { 15 get { return mPathName; } 16 set { mPathName = value; } 17 } 18 19 protected virtual string FileName 20 { 21 get { return mFileName; } 22 set { mFileName = value; } 23 } 24 25 protected virtual string FilePathName 26 { 27 get 28 { 29 return Path.Combine(PathName, FileName); 30 } 31 } 32 33 public virtual bool Load() 34 { 35 bool result = false; 36 try 37 { 38 string filePathName = this.FilePathName; 39 if (File.Exists(filePathName)) 40 { 41 this.LoadDocument(filePathName); 42 result = true; 43 } 44 } 45 catch(Exception ex) 46 { 47 //异常信息输出 48 } 49 return result; 50 } 51 52 public virtual bool Save() 53 { 54 bool result = false; 55 try 56 { 57 58 string pathName = this.PathName; 59 if (!Directory.Exists(pathName)) 60 { 61 Directory.CreateDirectory(PathName); 62 } 63 string tempFileName = "~" + FileName; 64 string tempfilePathName = Path.Combine(pathName, tempFileName); 65 this.SaveDocument(tempfilePathName); 66 string filePathName = Path.Combine(PathName, FileName); 67 if (File.Exists(filePathName)) 68 { 69 FileAttributes att = File.GetAttributes(filePathName); 70 if((att & FileAttributes.ReadOnly)== FileAttributes.ReadOnly) 71 { 72 File.SetAttributes(filePathName, att & ~FileAttributes.ReadOnly); 73 } 74 } 75 File.Copy(tempfilePathName, filePathName, true); 76 if (File.Exists(tempfilePathName)) 77 { 78 File.Delete(tempfilePathName); 79 } 80 result = true; 81 } 82 catch (Exception ex) 83 { 84 //异常信息输出 85 } 86 return result; 87 } 88 89 private void LoadDocument(string fileName) 90 { 91 try 92 { 93 XDocument doc = XDocument.Load(fileName); 94 this.ReadXml(doc); 95 } 96 catch(Exception ex) 97 { 98 //异常信息输出 99 } 100 } 101 102 private void SaveDocument(string fileName) 103 { 104 try 105 { 106 XDocument doc = new XDocument(); 107 this.WriteXml(doc); 108 doc.Save(fileName); 109 } 110 catch(Exception ex) 111 { 112 //异常信息输出 113 } 114 } 115 116 private void ReadXml(XDocument doc) 117 { 118 XElement root = doc.Root; 119 this.ReadXml(root); 120 } 121 122 private void WriteXml(XDocument doc) 123 { 124 XElement root = new XElement("root"); 125 doc.Add(root); 126 this.WriteXml(root); 127 } 128 129 protected abstract void ReadXml(XElement node); 130 131 protected abstract void WriteXml(XElement node); 132 } 133 134 public static class XMLSerializeExtensionClass 135 { 136 public static void Write(this XElement node,string name,string value) 137 { 138 node.SetAttributeValue(name, value); 139 } 140 141 public static string ReadString(this XElement node, string name, string defaultValue) 142 { 143 XAttribute att = node.Attribute(name); 144 return att != null ? att.Value : defaultValue; 145 } 146 147 public static void Write(this XElement node,string name,long value) 148 { 149 node.SetAttributeValue(name, value); 150 } 151 152 public static long ReadLong(this XElement node,string name,long defaultValue) 153 { 154 XAttribute att = node.Attribute(name); 155 return att != null ? Convert.ToInt64(att.Value) : defaultValue; 156 } 157 158 public static void Write(this XElement node,string name,decimal value) 159 { 160 node.SetAttributeValue(name, value); 161 } 162 163 public static decimal ReadDecimal(this XElement node,string name,decimal defaultValue) 164 { 165 XAttribute att = node.Attribute(name); 166 return att != null ? Convert.ToDecimal(att.Value) : defaultValue; 167 } 168 169 public static void Write(this XElement node ,string name,DateTime value) 170 { 171 node.SetAttributeValue(name, value); 172 } 173 174 public static DateTime ReadDateTime(this XElement node,string name,DateTime defaultValue) 175 { 176 XAttribute att = node.Attribute(name); 177 return att != null ? Convert.ToDateTime(att.Value) : defaultValue; 178 } 179 180 public static void Write(this XElement node,string name,int value) 181 { 182 node.SetAttributeValue(name, value); 183 } 184 185 public static int ReadInt(this XElement node,string name,int defaultValue) 186 { 187 XAttribute att = node.Attribute(name); 188 return att != null ? Convert.ToInt32(att.Value) : defaultValue; 189 } 190 191 public static void Write(this XElement node, string name,bool value) 192 { 193 node.SetAttributeValue(name, value); 194 } 195 196 public static bool ReadBoolean(this XElement node, string name, bool defaultValue) 197 { 198 XAttribute att = node.Attribute(name); 199 return att != null ? Convert.ToBoolean(att.Value) : defaultValue; 200 } 201 } 202 }