https://stackoverflow.com/questions/73127531/how-to-add-a-new-node-to-an-existing-xml-using-c-sharp

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;

using System.Text.Json;

using NUnit.Framework; //NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/73127531/how-to-add-a-new-node-to-an-existing-xml-using-c-sharp

    [XmlRoot("CountriesAndCities")]
    public class CountriesAndCities
    {
        public Country [] Name 		// Replace the List<Country> with Country []
        {
            get { return DictCountriesAndCities.Select(x => new Country { CountryName = x.Key, Cities = x.Value }).ToArray(); }
            set { DictCountriesAndCities = value?.ToDictionary(x => x.CountryName, x => x.Cities); }
        }
        [XmlIgnore]
        public Dictionary<string, List<string>> DictCountriesAndCities { get; set; }
    }

    public class Country
    {
        public string CountryName { get; set; }
        public List<string> Cities { get; set; }
    }

    public class TestClass
    {
        public static void Test()
        {
			Dictionary<string, List<string>> dictCountriesAndCities = new ()
			{
				["entry1"] = new() { "a", "b", "c" },
				["entry2"] = new() { "a", "b", "c" },
			};
				
			var fileName = "Question76137206.xml";

			TestWrite(dictCountriesAndCities, fileName);
			
			Console.WriteLine(File.ReadAllText(fileName));

			var dict2 = TestRead(fileName);
			
			Assert.AreEqual(dictCountriesAndCities.Count, dict2.Count);
			
			Assert.AreEqual(JsonSerializer.Serialize(dictCountriesAndCities), JsonSerializer.Serialize(dict2));
        }
		
		static void TestWrite(Dictionary<string, List<string>> dictCountriesAndCities, string fileName)
		{
			var sfd = new  { FileName = fileName, };

			var container = new CountriesAndCities
			{
				DictCountriesAndCities = dictCountriesAndCities,
			};
            using (var writer = XmlWriter.Create(sfd.FileName, new XmlWriterSettings { Indent = true })) // Indentation is for visualization purposes only
                (new XmlSerializer(typeof(CountriesAndCities))).Serialize(writer, container);
		}

		static Dictionary<string, List<string>> TestRead(string fileName)
		{
			Dictionary<string, List<string>> dictCountriesAndCities;
			
			var ofd = new  { FileName = fileName, };
			
            using (var reader = new StreamReader(ofd.FileName))
            {
                dictCountriesAndCities = ((new XmlSerializer(typeof(CountriesAndCities))).Deserialize(reader) as CountriesAndCities)
					?.DictCountriesAndCities;
            }
			
			return dictCountriesAndCities;
		}
    }

    public static class XmlSerializationHelper
    {
        public static T LoadFromXml<T>(this string xmlString, XmlSerializer serial = null)
        {
            serial = serial ?? new XmlSerializer(typeof(T));
            using (var reader = new StringReader(xmlString))
            {
				return (T)serial.Deserialize(reader);
            }
        }

        public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
        {
			return obj.GetXml(null, omitStandardNamespaces);
        }
		
        public static string GetXml<T>(this T obj, XmlSerializer serializer = null, bool omitStandardNamespaces = false)
        {
            XmlSerializerNamespaces ns = null;
            if (omitStandardNamespaces)
            {
                ns = new XmlSerializerNamespaces();
                ns.Add("", ""); // Disable the xmlns:xsi and xmlns:xsd lines.
            }			
            using (var textWriter = new StringWriter())
            {
                var settings = new XmlWriterSettings() { Indent = true }; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                    (serializer ?? new XmlSerializer(obj.GetType())).Serialize(xmlWriter, obj, ns);
                return textWriter.ToString();
            }
        }
    }
				
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1}, {2})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , Environment.Version, Environment.OSVersion);
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
	}
;