https://stackoverflow.com/questions/57287775/xdocument-write-specific-xelement-in-a-signle-line

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.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
using System.Reflection;

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

//https://stackoverflow.com/questions/57287775/xdocument-write-specific-xelement-in-a-signle-line

	public class CustomFormattingXmlTextWriter : XmlTextWriter
	{
		readonly Stack<Formatting> stack = new Stack<Formatting>();
	
        public CustomFormattingXmlTextWriter(TextWriter writer, int indentDepth) : base(writer) 
		{ 
			this.Formatting = Formatting.Indented; 
			this.IndentDepth = indentDepth;
		}
		
		int IndentDepth { get; }

		void OnElementStarted(string localName, string ns)
		{
			stack.Push(Formatting);
			// You could e.g. modify the logic here to check to see whether localName == CHILDELEMENT
			// if (localName == "CHILDELEMENT")
			if (stack.Count == IndentDepth+1)
				Formatting = Formatting.None;
		}

		void OnElementEnded()
		{
			var old = stack.Pop();
			if (old != Formatting)
				Formatting = old;
		}

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
			base.WriteStartElement(prefix, localName, ns);
			OnElementStarted(localName, ns);
        }

        public override void WriteEndElement()
        {
			base.WriteEndElement();
			OnElementEnded();
        }
		
        public override void WriteFullEndElement()
        {
			base.WriteEndElement();
			OnElementEnded();
        }
	}

	public static partial class XNodeExtensions
	{
		public static void SaveWithCustomFormatting(this XDocument doc, string filename, int indentDepth)
		{
			using (var textWriter = new StreamWriter(filename))
			using (var writer = new CustomFormattingXmlTextWriter(textWriter, indentDepth))
			{
				doc.Save(writer);
			}
		}
	}

    public class TestClass
    {
        public static void Test()
        {
			var fileName = "Question57287775.xml";
			
			var myDoc = new XDocument(new XElement("ROOTELEMENT",
												   new XElement("CHILDELEMENT",
																new XElement("INFO1", "Test a 1"),
																new XElement("INFO2", "Test a 2")),
												   new XElement("CHILDELEMENT",
																new XElement("INFO1", "Test b 1"),
																new XElement("INFO2", "Test b 2")),
												   new XElement("CHILDELEMENT")
												  ));

			myDoc.SaveWithCustomFormatting(fileName, 1);
			
			Console.WriteLine(File.ReadAllText(fileName));
        }
    }

						
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
			Console.WriteLine();

			try
			{
				TestClass.Test();
			}
			catch (Exception ex)
			{
				Console.WriteLine("Failed with unhandled exception: ");
				Console.WriteLine(ex);
				throw;
			}
		}
		
		public static string GetNetCoreVersion()
		{
			//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
			//
			var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
			var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
			int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
			if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
				return assemblyPath[netCoreAppIndex + 1];
			return null;
		}
	}
;