C # Read the base class implementation of XML files

Source: Internet
Author: User

Just to new units, learning their source code, the code to read and write the system configuration file of the XML code compared to the old, directly written in a system configuration class, no class splitting, causing the class is very large, at the same time, the operation of XML read and write operations are using setattribute and Node.attribute (name) method, therefore, the thought of combining the previous XML operation, completed a can read the XML file base class, easy to use later.

PS: Even if the old code, and currently do not dare to optimize, one is the level not, two is not afraid.

Using static extension classes, several frequently used types are extended to facilitate the reading and writing of data.

The classes that manipulate XML can inherit baselinqxmlfileinfo directly, only need to set the variable mpathname,mfilename of the protected type, and then rewrite the abstract method to implement the read operation of the XML document.

PS: The ability is limited, the wrong place please correct me, thank you.

  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 Mpathnam E Ten protected string mfilename; Protected virtual string PathName {get {return mpathname;} {mpathname = value;}             Protected virtual string FileName ({) {return mfilename;} 22             set {mfilename = value;} protected virtual String Filepathname 26 {27  Get Path.Combine {PathName, FileName); 30} 31} 32             Public virtual bool Load () {37 bool result = FALSE; {Filepathname string = this. Filepathname; if (file.exists (filepathname)) 40 {41 this. Loaddocument (filepathname); The result = true;             (Exception ex) 46 {47//exception information output 48 } return result;  The public virtual bool Save () () (). $ pathName = this. PathName; if (!                 Directory.Exists (PathName)) (Directory.CreateDirectory) (pathName); 62 } The string tempfilename = "~" + FileName; Tempfilepathname string = Path.Combine (PathName, tempfilename); this. SaveDocument (Tempfilepathname); Filepathname string = Path.Combine (PathName, FileName); if (file.exists (filepathname)) fileattributes att = file.gEtattributes (filepathname);                         if (att & fileattributes.readonly) = = fileattributes.readonly) 71 {72 File.setattributes (Filepathname, Att & ~fileattributes.readonly); File.Copy} (Tempfilepathname, Filepathname, true); if (file.exists (tempfilepathname)) The File.delete (TempFilePath Name); The result = true;             Bayi} (Exception ex) 83 {84//exception information output 85} 86 return result;                  The Loaddocument (string fileName) of the private void, {92) {93 XDocument doc = Xdocument.load (fileName); 94 this. READXML (DOC); (Exception ex) 97 {98//exception information output 99             }100}101 102 private void SaveDocument (string fileName) 103 {104 try105 {106 XDocument doc = new XDocument (); 107 this. WRITEXML (DOC); 108 Doc.             Save (fileName); 109}110 catch (Exception ex) 111 {112//exception information output 113 }114}115, private void ReadXml (XDocument doc) 117 {118 XElement root = Doc. root;119 this. READXML (Root),}121 122 private void WriteXml (XDocument doc) 123 {124 XElement root = New XElement ("root"); ADD (root); 126 this. WRITEXML (Root), 127}128 129 protected abstract void ReadXml (XElement node), 131 protected Abstr ACT void WriteXml (XElement node),}133 134 public static class XMLSerializeExtensionClass135 {136 p ublic 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 nam E, 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);}151 public static Long Readlong (this XElement node,string Name,lon G 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) 1 The. 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 ). {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. 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 D Efaultvalue) 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}

C # Read the base class implementation of XML files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.