https://stackoverflow.com/questions/72696326/parse-and-save-xml-without-replacing

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 NUnit.Framework; //NUnit.Framework.AssertionException

//https://stackoverflow.com/questions/72696326/parse-and-save-xml-without-replacing

    public class TestClass
    {
        public static void Test()
        {
			var xmlString = @"<?xml version=""1.0"" encoding=""utf-8""?><Foo Name=""a->b""></Foo>";

			var doc = XDocument.Parse(xmlString);
			
			Assert.AreEqual("a->b", doc.Root.Attribute("Name").Value); // Passes sucdcessfully

			foreach (var element in doc.Root.DescendantsAndSelf())
			{
				Console.Write("Attributes for element {0}: ", element.Name);
				int i = 0;
				foreach (var attr in doc.Root.Attributes())
					Console.Write("\"{0}\": \"{1}\"{2}", attr.Name, attr.Value, i++ == 0 ? "" : ", ");
				Console.WriteLine();
			}
			
			Console.WriteLine("\nRe-serialiezd XDocument:");
			Console.WriteLine(doc.Root);
		}
    }

	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;
			}
		}
	}
;