0

I have an XML as below:

    <configuration>
     <AppSettings>
       <add key="FilePath" value="c:\test" />
       <add key="domain" value="google" />
     </AppSettings>
   </configuration>

I want a quick c:\ code to read App Settings from .XML file. We don't want to use ConfigurationManager and App.config route as these keys are being shared via multiple projects/applications so requirement is to keep it on shared location.

e.g. a static class method as below: settings.GetAppKey("FilePath") shall return value e.g. c:\test

user2739418
  • 1,623
  • 5
  • 29
  • 51
  • 1
    @TomW OP has already stated _"We don't want to use `ConfigurationManager`..."_ –  Nov 20 '20 at 09:06
  • Which is why I retracted the close vote. – Tom W Nov 20 '20 at 09:08
  • In any case, you can still use ConfigurationManager [with a custom configuration file](https://stackoverflow.com/questions/6341906/read-custom-configuration-file-in-c-sharp-framework-4-0) – Tom W Nov 20 '20 at 09:09
  • @TomW actually it was the _comment_ that I noticed. It's still sitting there too :) –  Nov 20 '20 at 09:12
  • I'm not sure if _"We `don't` want to use ... as these `keys are being shared` via multiple projects/applications so `requirement is to keep it on shared location`"_ makes sense. **a)** If you mean **uniqueness** then to ensure your key won't be used by anything else give it a _unique name_. **b)** Same goes if you want to supply a different value for an _existing key_. **c)** However if you mean you want to store setttings in a **different folder** to the .exe you might want to consider _Isolated Storage_. It can be unique using a combination of _domain, assembly_ and _machine/user_. –  Nov 20 '20 at 09:16
  • I am not sure shared App.config can be used for web/mvc projects. Its only applicable for EXE's. – user2739418 Nov 20 '20 at 09:21

1 Answers1

1

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"));
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92