Com.H.Extensions.Configuration.Xml 8.0.0

dotnet add package Com.H.Extensions.Configuration.Xml --version 8.0.0                
NuGet\Install-Package Com.H.Extensions.Configuration.Xml -Version 8.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="Com.H.Extensions.Configuration.Xml" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Com.H.Extensions.Configuration.Xml --version 8.0.0                
#r "nuget: Com.H.Extensions.Configuration.Xml, 8.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 Com.H.Extensions.Configuration.Xml as a Cake Addin
#addin nuget:?package=Com.H.Extensions.Configuration.Xml&version=8.0.0

// Install Com.H.Extensions.Configuration.Xml as a Cake Tool
#tool nuget:?package=Com.H.Extensions.Configuration.Xml&version=8.0.0                

Com.H.Extensions.Configuration.Xml

A thread-safe library that extends the Microsoft.Extensions.Configuration library to support reading and writing configuration values to XML configuration files.

Example 1

This example demonstrates how to write a value to a configuration object and save it to automatically create a new configuration file.

To run this sample, you need to:

  1. Create a new console application
  2. Add NuGet package Com.H.Extensions.Configuration.Xml
  3. Copy and paste the following code into your Program.cs file:
using Microsoft.Extensions.Configuration;
using Com.H.Extensions.Configuration.Xml;

var builder = new ConfigurationBuilder()
    .AddXmlFileWithSave("config.xml", optional: true, reloadOnChange: true);
// note: if you set optional to false, the library will throw an exception if the file does not exist
// otherwise it will create a new file with the specified name when you call the Save() method

IConfiguration configuration = builder.Build();

// write a value
configuration["key"] = "test value";

// save the changes
configuration.Save();

Note: the above code will create a new file config.xml in the output directory of your console application. The file will contain the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <key>test value</key>
</configuration>

Example 2

This example demonstrates how to read a value from a configuration file, update it, and save the changes.

using Microsoft.Extensions.Configuration;
using Com.H.Extensions.Configuration.Xml;

var builder = new ConfigurationBuilder()
	.AddXmlFileWithSave("config.xml", optional: true, reloadOnChange: true);

IConfiguration configuration = builder.Build();

// read a value
var value = configuration["key"];
Console.WriteLine(value);

// updating the value
configuration["key"] = "new value";

// save the changes
configuration.Save();

Example 3

This example demonstrates how to save a value encapsulated in a CDATA section. This is useful when you want to save a value that contains special characters.

using Microsoft.Extensions.Configuration;
using Com.H.Extensions.Configuration.Xml;

var builder = new ConfigurationBuilder()
	.AddXmlFileWithSave("config.xml", optional: true, reloadOnChange: true);

IConfiguration configuration = builder.Build();

// write a value
configuration.SetWithCData("key", "test value with some special characters &>#");

// save the changes
configuration.Save();

Note: the above code will create a new file config.xml in the output directory of your console application. The file will contain the following content:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <key><![CDATA[test value with some special characters &>#]]></key>
</configuration>

Example 4

This example demonstrates how to read a value encapsulated in a CDATA section.

using Microsoft.Extensions.Configuration;
using Com.H.Extensions.Configuration.Xml;

var builder = new ConfigurationBuilder()
	.AddXmlFileWithSave("config.xml", optional: true, reloadOnChange: true);

IConfiguration configuration = builder.Build();

// read a value
var value = configuration["key"];
Console.WriteLine(value);

// notice that there is no need to do anything special to read a value encapsulated in a CDATA section
// the library will automatically handle it. All you need to to is to read the value as usual

// updating the value
configuration["key"] = "new value with special characters #&>";

// notice that there is no need to do anything special to update a value encapsulated in a CDATA section
// the library will automatically handle it. All you need to to is to update the value as usual.
// You only need to use SetWithCData method when it's the first time you write the value.

// save the changes
configuration.Save();

Note: Notice that there was no need to do anything special to read or update a value encapsulated in a CDATA section. The library will automatically handle it if the .xml file contains a value encapsulated in a CDATA section. However, if you are writing a new key to the configuration file that never been written before, you need to use SetWithCData method to encapsulate the value in a CDATA section only once. After that, you can read and update the value as usual.

Example 5

This example demonstrates how to read a value from a configuration file and the value gets updated outside the application and the application automatically reloads the updated value.

using Microsoft.Extensions.Configuration;
using Com.H.Extensions.Configuration.Xml;

var builder = new ConfigurationBuilder()
	.AddXmlFileWithSave("config.xml", optional: true, reloadOnChange: true);

IConfiguration configuration = builder.Build();

bool continueLoop = true;

while (continueLoop)
{
    string settingValue = configuration["key"];
    if (!string.IsNullOrEmpty(settingValue))
    {
        Console.WriteLine($"key: {settingValue}");
    }
    Console.WriteLine("press `c` to exit, or any key to display re-read the value of `test`");
    var key = Console.ReadKey();
    if (key.KeyChar == 'c')
    {
        continueLoop = false;
    }
}

Note: With the above example, you can open the config.xml file and update the value of key and save the file. The application will automatically reload the updated value and prints it to the console.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
8.0.0 90 10/16/2024

Initial release