@@ -64,9 +64,16 @@ public static void Set(string id, object value, Assembly calling = null)
64
64
private static T GetSettingFromXml < T > ( XmlDocument doc , string id , T failsafe )
65
65
{
66
66
var v = doc . SelectSingleNode ( $@ "/Settings/{ id } ") ;
67
- var result = v == null ? failsafe : ( T ) Convert . ChangeType ( v . InnerText , typeof ( T ) ) ;
68
67
69
- return result ;
68
+ try
69
+ {
70
+ var result = v == null ? failsafe : ( T ) Convert . ChangeType ( v . InnerText , typeof ( T ) ) ;
71
+ return result ;
72
+ }
73
+ catch ( Exception )
74
+ {
75
+ return failsafe ;
76
+ }
70
77
}
71
78
72
79
private static void WriteSettingToXml ( XmlDocument doc , string id , object value )
@@ -81,7 +88,7 @@ private static void WriteSettingToXml(XmlDocument doc, string id, object value)
81
88
{
82
89
var node = doc . CreateNode ( XmlNodeType . Element , id , doc . NamespaceURI ) ;
83
90
node . InnerText = value . ToString ( ) ;
84
- doc . SelectSingleNode ( @"/Settings" ) . AppendChild ( node ) ;
91
+ doc . SelectSingleNode ( @"/Settings" ) ? . AppendChild ( node ) ;
85
92
}
86
93
87
94
doc . Save ( new Uri ( doc . BaseURI ) . LocalPath ) ;
@@ -97,7 +104,22 @@ private static XmlDocument GetConfigFile(string file)
97
104
CreateNewConfig ( file ) ;
98
105
99
106
var doc = new XmlDocument ( ) ;
100
- doc . Load ( file ) ;
107
+ try
108
+ {
109
+ doc . Load ( file ) ;
110
+ }
111
+ catch ( XmlException )
112
+ {
113
+ CreateNewConfig ( file ) ;
114
+ doc . Load ( file ) ;
115
+ }
116
+
117
+ if ( doc . SelectSingleNode ( @"/Settings" ) == null )
118
+ {
119
+ CreateNewConfig ( file ) ;
120
+ doc . Load ( file ) ;
121
+ }
122
+
101
123
FileCache . Add ( file , doc ) ;
102
124
return doc ;
103
125
}
@@ -110,6 +132,8 @@ private static void CreateNewConfig(string file)
110
132
writer . WriteStartElement ( "Settings" ) ;
111
133
writer . WriteEndElement ( ) ;
112
134
writer . WriteEndDocument ( ) ;
135
+
136
+ writer . Flush ( ) ;
113
137
}
114
138
}
115
139
}
0 commit comments