C # base class implementation for reading XML files,

Source: Internet
Author: User

C # base class implementation for reading XML files,

At the beginning of the new unit, I learned their source code. The XML code for reading and writing system configuration files in the Code is relatively old and written directly in a system configuration class without class splitting, the class is huge. At the same time, the read and write operations for XML operations use SetAttribute and node. attribute (name) method. Therefore, a base class that can read XML files is completed in combination with the XML operations previously performed, which is convenient for future use.

PS: Even the old-fashioned Code does not dare to be optimized at present. First, the level is not good, and second, it is not.

The static extension class extends several frequently used types to facilitate data reading and writing.

The XML class can inherit the BaseLinqXmlFileInfo directly. You only need to set the protected type variable mPathName and mFileName, and then rewrite the abstract method to read the XML document.

PS: limited capacity. please correct me if you have any mistakes. 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 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 // Exception information output 48} 49 re Turn 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 // Exception information output 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} 9 6 catch (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 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 XMLSerializeExtensionClass135 {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}

 

Related Article

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.