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
<PackageReference Include="Com.H.Extensions.Configuration.Xml" Version="8.0.0" />
paket add Com.H.Extensions.Configuration.Xml --version 8.0.0
#r "nuget: Com.H.Extensions.Configuration.Xml, 8.0.0"
// 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:
- Create a new console application
- Add NuGet package Com.H.Extensions.Configuration.Xml
- 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 ofkey
and save the file. The application will automatically reload the updated value and prints it to the console.
Product | Versions 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. |
-
net8.0
- Microsoft.Extensions.Configuration.Xml (>= 8.0.1)
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 | 92 | 10/16/2024 |
Initial release