ConfManager 4.0.0

dotnet add package ConfManager --version 4.0.0                
NuGet\Install-Package ConfManager -Version 4.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ConfManager" Version="4.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add ConfManager --version 4.0.0                
#r "nuget: ConfManager, 4.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install ConfManager as a Cake Addin
#addin nuget:?package=ConfManager&version=4.0.0

// Install ConfManager as a Cake Tool
#tool nuget:?package=ConfManager&version=4.0.0                

ConfManager

To save time on the implementation of your configuration files, ConfManager allows you to serialize an object in an XML file.

The idea of using a serialized object in an XML file offers the possibility of setting up complex configurations (with array/list for example).

How to use it?

Here is the object we will use to manage the settings of my application:

public class MySettings
{
   public string ConnectionName { get; set; }
   public bool Enabled { get; set; }
   public string Username { get; set; }
   public string Password { get; set; }
   public string Server { get; set; }
   public int Port { get; set; }
   public List<string> Paths;
}

We will save the object once to build the structure of the XML file. This will allow us to modify the configuration manually without worrying if the XML is valid or not. We will remove this code later when the file is created.

// Build Settings
MySettings settings = new MySettings
{
   ConnectionName = "Production server",
   Enabled = true,
   Username = "admin",
   Password = "123456789",
   Server = "prod.server.com",
   Port = 8080,
   Paths = new List<string> {@"C:\Temp", @"\\quality.server.com\shared$"}
};

// Save
ConfManager.Save("app.settings.xml", settings);
// or
settings.SaveSettings("app.settings.xml");

And.. ..here is the Settings file:

<?xml version="1.0" encoding="utf-8"?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Paths>
   <string>C:\Temp</string>
   <string>\\quality.server.com\shared$</string>
 </Paths>
 <ConnectionName>Production server</ConnectionName>
 <Enabled>true</Enabled>
 <Username>admin</Username>
 <Password>123456789</Password>
 <Server>prod.server.com</Server>
 <Port>8080</Port>
</MySettings>

Now to load the Settings we will use the Load method:

MySettings mySettings = ConfManager.Load<MySettings>("app.settings.xml");

// We can use the settings
// Server server = new Server(mySettings.Server, mySettings.Port);
// ...

A simple serialization process

No secret, this library uses the following .NET methods to serialize and deserialize:

Here is what it looks like if you want to copy/past:

Serialize

string path = "myfile.xml";
YourObject obj = new YourObject();

// ...

XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
using (StreamWriter streamWriter = new StreamWriter(path))
{
   xmlSerializer.Serialize(streamWriter, obj);
}

Deserialize

string path = "myfile.xml";
YourObject obj;

XmlSerializer xmlSerializer = new XmlSerializer(typeof(YourObject));
using (FileStream fileStream = new FileStream(path, FileMode.Open))
{
   YourObject = (YourObject)xmlSerializer.Deserialize(fileStream);
}

Limitations

Based on the MSDN, here is the limitation of the Serialization :

You must use:

  • Public properties (read and write) & fields
  • Public constructor (without args)
  • No dictionnary
  • All types to be known
  • ...

License

MIT

Product Compatible and additional computed target framework versions.
.NET Framework net35 is compatible.  net40 was computed.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.0 438 3/18/2022

It was necessary to have write permission on the file to load a configuration.