Add classes to deserialize like below.
[XmlRoot(ElementName = "configuration")]
public class ApplicationConfiguration
{
private Dictionary<string, string> _appSettings = new Dictionary<string, string>();
[XmlArray(ElementName = "appSettings")]
[XmlArrayItem(ElementName = "add")]
public List<AppSetting> ToolsGroups { get; set; }
public string GetAppValue(string key)
{
return ToolsGroups.Where(x => x.Key.Equals(key)).FirstOrDefault().Value;
}
}
public class AppSetting
{
[XmlAttribute(AttributeName = "key")]
public string Key { get; set; }
[XmlAttribute(AttributeName = "value")]
public string Value { get; set; }
}
Then deserialize and use it.
bool isAppConfigFormatCorrect = false;
string appConfigFile = Path.Combine(@"..\..\..\..\Config", "app.config");
ApplicationConfiguration appConfig = new ApplicationConfiguration();
try
{
XmlDocument xmlDocumentAppConfig = new XmlDocument();
xmlDocumentAppConfig.Load(appConfigFile);
XmlSerializer xmlSerializer = new XmlSerializer(typeof(ApplicationConfiguration));
XmlReader xmlReader = XmlReader.Create(appConfigFile);
appConfig = (ApplicationConfiguration)xmlSerializer.Deserialize(xmlReader);
isAppConfigFormatCorrect = true;
}
catch (Exception ec)
{
MessageBox.Show($"Structure of the '{appConfigFile}' file is incorrect." + Environment.NewLine + $"Error Message: {ec.Message}");
}
if (isAppConfigFormatCorrect)
{
Debug.Print(appConfig.GetAppValue("FilePath"));
}