你的位置:首页 > ASP.net教程

[ASP.net教程]C#读取XML文件的基类实现


刚到新单位,学习他们的源代码,代码里读写系统配置文件的

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

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

操作

PS:能力有限,有不对之处请指正,谢谢。

 1 using System; 2 using System.IO; 3 using System. 4  5 namespace Common 6 { 7   public abstract class BaseLinq 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.Read 95       } 96       catch(Exception ex) 97       { 98         //异常信息输出 99       }100     }101 102     private void SaveDocument(string fileName)103     {104       try105       {106         XDocument doc = new XDocument();107         this.Write108         doc.Save(fileName);109       }110       catch(Exception ex)111       {112         //异常信息输出113       }114     }115 116     private void Read117     {118       XElement root = doc.Root;119       this.Read120     }121 122     private void Write123     {124       XElement root = new XElement("root");125       doc.Add(root);126       this.Write127     }128 129     protected abstract void Read130 131     protected abstract void Write132   }133 134   public static class 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 }