[BUG] XmlException invalid character in config (#199), version 1.0.19.2

This commit is contained in:
Markus Hofknecht 2021-10-01 20:24:51 +02:00
parent 76492e11dd
commit 520e056f6f
2 changed files with 76 additions and 45 deletions

View file

@ -39,5 +39,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.19.1")] [assembly: AssemblyVersion("1.0.19.2")]
[assembly: AssemblyFileVersion("1.0.19.1")] [assembly: AssemblyFileVersion("1.0.19.2")]

View file

@ -233,13 +233,15 @@ namespace SystemTrayMenu.Properties
XDocument configXml; XDocument configXml;
if (IsConfigPathAssembly()) if (IsConfigPathAssembly())
{ {
configXml = XDocument.Load(ConfigPathAssembly); configXml = LoadOrGetNew(ConfigPathAssembly);
} }
else else
{ {
configXml = XDocument.Load(UserConfigPath); configXml = LoadOrGetNew(UserConfigPath);
} }
if (configXml != null)
{
// get all of the <setting name="..." serializeAs="..."> elements. // get all of the <setting name="..." serializeAs="..."> elements.
IEnumerable<XElement> settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName).Elements(Setting); IEnumerable<XElement> settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName).Elements(Setting);
@ -256,6 +258,32 @@ namespace SystemTrayMenu.Properties
SettingsDictionary.Add(element.Attribute(NameOf).Value, newSetting); SettingsDictionary.Add(element.Attribute(NameOf).Value, newSetting);
} }
} }
}
private XDocument LoadOrGetNew(string path)
{
XDocument xDocument = null;
try
{
xDocument = XDocument.Load(path);
}
catch (Exception exceptionWarning)
{
Log.Warn($"Could not load {path}", exceptionWarning);
try
{
File.Delete(path);
CreateEmptyConfigIfNotExists(path);
xDocument = XDocument.Load(path);
}
catch (Exception exceptionError)
{
Log.Error($"Could not delete and create {path}", exceptionError);
}
}
return xDocument;
}
/// <summary> /// <summary>
/// Saves the in memory dictionary to the user config file. /// Saves the in memory dictionary to the user config file.
@ -263,18 +291,20 @@ namespace SystemTrayMenu.Properties
private void SaveValuesToFile() private void SaveValuesToFile()
{ {
// load the current xml from the file. // load the current xml from the file.
XDocument import; XDocument configXml;
if (IsConfigPathAssembly()) if (IsConfigPathAssembly())
{ {
import = XDocument.Load(ConfigPathAssembly); configXml = LoadOrGetNew(ConfigPathAssembly);
} }
else else
{ {
import = XDocument.Load(UserConfigPath); configXml = LoadOrGetNew(UserConfigPath);
} }
if (configXml != null)
{
// get the settings group (e.g. <Company.Project.Desktop.Settings>) // get the settings group (e.g. <Company.Project.Desktop.Settings>)
XElement settingsSection = import.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName); XElement settingsSection = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName);
// iterate though the dictionary, either updating the value or adding the new setting. // iterate though the dictionary, either updating the value or adding the new setting.
foreach (KeyValuePair<string, SettingStruct> entry in SettingsDictionary) foreach (KeyValuePair<string, SettingStruct> entry in SettingsDictionary)
@ -298,10 +328,11 @@ namespace SystemTrayMenu.Properties
if (IsConfigPathAssembly()) if (IsConfigPathAssembly())
{ {
import.Save(ConfigPathAssembly); configXml.Save(ConfigPathAssembly);
} }
import.Save(UserConfigPath); configXml.Save(UserConfigPath);
}
} }
/// <summary> /// <summary>