????????μ?λ?????????????????????д???????????XML??????????????д?????????????????н??????????????????????????XML???д???????????SetAttribute??node.Attribute(name)???????????????????????XML????????????????????XML??????????????????á?
????PS??????????????????????????????????????У?????????
??????t?????????????????????????????????????????д??
????????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 }