Skip to content

Commit

Permalink
[BUG] XmlException invalid character in config (#199), version 1.0.19.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofknecht committed Oct 1, 2021
1 parent 76492e1 commit 520e056
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.19.1")]
[assembly: AssemblyFileVersion("1.0.19.1")]
[assembly: AssemblyVersion("1.0.19.2")]
[assembly: AssemblyFileVersion("1.0.19.2")]
107 changes: 69 additions & 38 deletions Properties/CustomSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,56 @@ private void LoadValuesFromFile()
XDocument configXml;
if (IsConfigPathAssembly())
{
configXml = XDocument.Load(ConfigPathAssembly);
configXml = LoadOrGetNew(ConfigPathAssembly);
}
else
{
configXml = XDocument.Load(UserConfigPath);
configXml = LoadOrGetNew(UserConfigPath);
}

// get all of the <setting name="..." serializeAs="..."> elements.
IEnumerable<XElement> settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName).Elements(Setting);
if (configXml != null)
{
// get all of the <setting name="..." serializeAs="..."> elements.
IEnumerable<XElement> settingElements = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName).Elements(Setting);

// iterate through, adding them to the dictionary, (checking for nulls, xml no likey nulls)
// using "String" as default serializeAs...just in case, no real good reason.
foreach (XElement element in settingElements)
// iterate through, adding them to the dictionary, (checking for nulls, xml no likey nulls)
// using "String" as default serializeAs...just in case, no real good reason.
foreach (XElement element in settingElements)
{
SettingStruct newSetting = new SettingStruct()
{
Name = element.Attribute(NameOf) == null ? string.Empty : element.Attribute(NameOf).Value,
SerializeAs = element.Attribute(SerializeAs) == null ? "String" : element.Attribute(SerializeAs).Value,
Value = element.Value ?? string.Empty,
};
SettingsDictionary.Add(element.Attribute(NameOf).Value, newSetting);
}
}
}

private XDocument LoadOrGetNew(string path)
{
XDocument xDocument = null;
try
{
xDocument = XDocument.Load(path);
}
catch (Exception exceptionWarning)
{
SettingStruct newSetting = new SettingStruct()
Log.Warn($"Could not load {path}", exceptionWarning);
try
{
Name = element.Attribute(NameOf) == null ? string.Empty : element.Attribute(NameOf).Value,
SerializeAs = element.Attribute(SerializeAs) == null ? "String" : element.Attribute(SerializeAs).Value,
Value = element.Value ?? string.Empty,
};
SettingsDictionary.Add(element.Attribute(NameOf).Value, newSetting);
File.Delete(path);
CreateEmptyConfigIfNotExists(path);
xDocument = XDocument.Load(path);
}
catch (Exception exceptionError)
{
Log.Error($"Could not delete and create {path}", exceptionError);
}
}

return xDocument;
}

/// <summary>
Expand All @@ -263,45 +291,48 @@ private void LoadValuesFromFile()
private void SaveValuesToFile()
{
// load the current xml from the file.
XDocument import;
XDocument configXml;
if (IsConfigPathAssembly())
{
import = XDocument.Load(ConfigPathAssembly);
configXml = LoadOrGetNew(ConfigPathAssembly);
}
else
{
import = XDocument.Load(UserConfigPath);
configXml = LoadOrGetNew(UserConfigPath);
}

// get the settings group (e.g. <Company.Project.Desktop.Settings>)
XElement settingsSection = import.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName);

// iterate though the dictionary, either updating the value or adding the new setting.
foreach (KeyValuePair<string, SettingStruct> entry in SettingsDictionary)
if (configXml != null)
{
XElement setting = settingsSection.Elements().FirstOrDefault(e => e.Attribute(NameOf).Value == entry.Key);
if (setting == null)
// get the settings group (e.g. <Company.Project.Desktop.Settings>)
XElement settingsSection = configXml.Element(Config).Element(UserSettings).Element(typeof(Settings).FullName);

// iterate though the dictionary, either updating the value or adding the new setting.
foreach (KeyValuePair<string, SettingStruct> entry in SettingsDictionary)
{
// this can happen if a new setting is added via the .settings designer.
XElement newSetting = new XElement(Setting);
newSetting.Add(new XAttribute(NameOf, entry.Value.Name));
newSetting.Add(new XAttribute(SerializeAs, entry.Value.SerializeAs));
newSetting.Value = entry.Value.Value ?? string.Empty;
settingsSection.Add(newSetting);
XElement setting = settingsSection.Elements().FirstOrDefault(e => e.Attribute(NameOf).Value == entry.Key);
if (setting == null)
{
// this can happen if a new setting is added via the .settings designer.
XElement newSetting = new XElement(Setting);
newSetting.Add(new XAttribute(NameOf, entry.Value.Name));
newSetting.Add(new XAttribute(SerializeAs, entry.Value.SerializeAs));
newSetting.Value = entry.Value.Value ?? string.Empty;
settingsSection.Add(newSetting);
}
else
{
// update the value if it exists.
setting.Value = entry.Value.Value ?? string.Empty;
}
}
else

if (IsConfigPathAssembly())
{
// update the value if it exists.
setting.Value = entry.Value.Value ?? string.Empty;
configXml.Save(ConfigPathAssembly);
}
}

if (IsConfigPathAssembly())
{
import.Save(ConfigPathAssembly);
configXml.Save(UserConfigPath);
}

import.Save(UserConfigPath);
}

/// <summary>
Expand Down

0 comments on commit 520e056

Please sign in to comment.