In C#, To represent key-value pairs in XML, you typically use elements and attributes. Here's a basic example of how you can store key-value pairs in XML.
Step 1. XML file look.
<?xml version="1.0" encoding="utf-8"?>
<KeyPairValueSettings>
<KeyPairValueSetting key="Key1" value="Demo example 1" />
<KeyPairValueSetting key="Key2" value="Demo example 2" />
<KeyPairValueSetting key="Key3" value="Demo example 3" />
<KeyPairValueSetting key="Key4" value="Demo example 4" />
<KeyPairValueSetting key="Key5" value="" />
</KeyPairValueSettings>
Step 2. Create an interface for it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfXamlSettingsFile
{
public interface ISettingsHelper
{
bool SetValue(eXmlSettingsKeys keyToUpdate, string newValue);
string GetValue(eXmlSettingsKeys key);
}
}
Step 3. Create Enum class.
public enum eXmlSettingsKeys
{
Key1,
Key2,
Key3,
Key4,
Key5,
}
Step 4. Create implementation for the interface “ISettingsHelper”
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace WpfXamlSettingsFile
{
public class SettingsHelper : ISettingsHelper
{
private static Dictionary<eXmlSettingsKeys, string> dicXmlSettings;
private static string initialDirectoryPath = @"D:\TestingProjects\XmlSetting";
private static string filePath = @"D:\TestingProjects\XmlSetting\XmlSetiings.xml";
private static readonly object lockObject = new object();
private static XmlDocument xmlGlobalDocInstance = null;
public static void LoadSettings()
{
xmlGlobalDocInstance = new XmlDocument();
if (!File.Exists(filePath))
{
CreateAndSaveXmlDocument();
}
if (File.Exists(filePath))
{
foreach (eXmlSettingsKeys key in Enum.GetValues(typeof(eXmlSettingsKeys)))
{
bool result = IsKeyExistInXml(key);
if (!result)
{
AddNewKeyToXml(key.ToString());
}
}
LoadXmlSettingDictionary();
}
}
private static void LoadXmlSettingDictionary()
{
var settingResult = XmlSettingsFromFile(filePath);
if (settingResult != null)
{
dicXmlSettings = settingResult;
}
}
private static bool IsKeyExistInXml(eXmlSettingsKeys keyToCheck)
{
if (System.IO.File.Exists(filePath))
{
xmlGlobalDocInstance.Load(filePath);
foreach (XmlNode node in xmlGlobalDocInstance.SelectNodes("//KeyPairValueSetting"))
{
if (Enum.TryParse(node.Attributes["key"].Value, out eXmlSettingsKeys keyInXml))
{
if (keyInXml == keyToCheck)
{
return true;
}
}
}
}
return false;
}
private static void AddNewKeyToXml(string newKey)
{
if (System.IO.File.Exists(filePath))
{
xmlGlobalDocInstance.Load(filePath);
}
else
{
XmlElement rootElement = xmlGlobalDocInstance.CreateElement("KeyPairValueSettings");
xmlGlobalDocInstance.AppendChild(rootElement);
}
XmlElement newSettingElement = xmlGlobalDocInstance.CreateElement("KeyPairValueSetting");
newSettingElement.SetAttribute("key", newKey);
newSettingElement.SetAttribute("value", "");
xmlGlobalDocInstance.DocumentElement?.AppendChild(newSettingElement);
xmlGlobalDocInstance.Save(filePath);
}
private static void CreateAndSaveXmlDocument()
{
if (!Directory.Exists(initialDirectoryPath))
{
Directory.CreateDirectory(initialDirectoryPath);
}
XmlDeclaration xmlDeclaration = xmlGlobalDocInstance.CreateXmlDeclaration("1.0", "utf-8", null);
xmlGlobalDocInstance.AppendChild(xmlDeclaration);
XmlElement rootElement = xmlGlobalDocInstance.CreateElement("KeyPairValueSettings");
xmlGlobalDocInstance.AppendChild(rootElement);
xmlGlobalDocInstance.Save(filePath);
}
private static Dictionary<eXmlSettingsKeys, string> XmlSettingsFromFile(string filePath)
{
Dictionary<eXmlSettingsKeys, string> keyValuePairs = new Dictionary<eXmlSettingsKeys, string>();
if (File.Exists(filePath))
{
xmlGlobalDocInstance.Load(filePath);
foreach (XmlNode node in xmlGlobalDocInstance.SelectNodes("//KeyPairValueSetting"))
{
string key = node.Attributes["key"].Value;
string value = node.Attributes["value"].Value;
keyValuePairs[(eXmlSettingsKeys)Enum.Parse(typeof(eXmlSettingsKeys), key)] = value;
}
}
return keyValuePairs;
}
public string GetValue(eXmlSettingsKeys key)
{
string retValue = "";
lock (lockObject)
{
if (dicXmlSettings.ContainsKey(key))
{
retValue = dicXmlSettings[key];
}
}
return retValue;
}
public bool SetValue(eXmlSettingsKeys keyToUpdate, string newValue)
{
bool retValue = false;
dicXmlSettings[keyToUpdate] = newValue;
lock (lockObject)
{
XmlElement rootElement;
if (System.IO.File.Exists(filePath))
{
xmlGlobalDocInstance.Load(filePath);
rootElement = xmlGlobalDocInstance.DocumentElement;
rootElement?.RemoveAll(); // Clear existing settings
}
else
{
rootElement = xmlGlobalDocInstance.CreateElement("KeyPairValueSettings");
xmlGlobalDocInstance.AppendChild(rootElement);
}
foreach (var entry in dicXmlSettings)
{
XmlElement settingElement = xmlGlobalDocInstance.CreateElement("KeyPairValueSetting");
settingElement.SetAttribute("key", entry.Key.ToString());
settingElement.SetAttribute("value", entry.Value);
rootElement?.AppendChild(settingElement);
}
xmlGlobalDocInstance.AppendChild(rootElement);
xmlGlobalDocInstance.Save(filePath);
}
return retValue;
}
}
}


Join the conversation! Your thoughts help the community grow.