C# utility class Serialize objects to and from XML strings.

namespace XmlSerialize
{
    using System;
    using System.Text;
    using System.Diagnostics;
    using System.IO;
    using System.Xml;
    using SystemXmlSerializer = System.Xml.Serialization.XmlSerializer;

    public static class XmlSerializationUtils
    {
        private static readonly XmlWriterSettings XmlSettings;

        static XmlSerializationUtils()
        {
            XmlSettings = new XmlWriterSettings
            {
                Encoding = Encoding.UTF8,
                Indent = true,
                IndentChars = "  ", // indent using 2 spaces
                CheckCharacters = false,
                OmitXmlDeclaration = true,
                ConformanceLevel = ConformanceLevel.Auto
            };
        }

        public static string Serialize(object obj)
        {
            Debug.Assert(obj != null);

            using (var sw = new StringWriter())
            {
                Serialize(obj, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

        private static void Serialize(object obj, TextWriter output)
        {
            Debug.Assert(obj != null);
            Debug.Assert(output != null);

            var writer = XmlWriter.Create(output, XmlSettings);

            try
            {
                var serializer = new SystemXmlSerializer(obj.GetType());
                serializer.Serialize(writer, obj);
                writer.Flush();
            }
            finally
            {
                writer.Close();
            }
        }

        public static T Deserialize<T>(string serializedXml)
        {
            return (T) Deserialize(serializedXml, typeof(T));
        }

        private static object Deserialize(string serializedXml, Type type)
        {
            var serializer = new SystemXmlSerializer(type);

            object result;

            using (TextReader reader = new StringReader(serializedXml))
            {
                result = serializer.Deserialize(reader);
            }

            return result;
        }
    }
}


// ----------------------------------------------------------------
// Example Usage
// ----------------------------------------------------------------

namespace XmlSerializerConsole
{
    using System;
    using System.Collections.Generic;
    using XmlSerialize;

    public class Person
    {
        public Person() => Friends = new List<Person>();
        public string Name { get; set; }
        public DateTime BirthDay { get; set; }
        public List<Person> Friends { get; set; }
    }

    internal static class Program
    {
        private static Person CreatePerson()
        {
            var person = new Person
            {
                Name = "Jon LaBelle",
                BirthDay = DateTime.UtcNow
            };

            person.Friends.Add(new Person {Name = "Friend 1", BirthDay = DateTime.UtcNow});
            person.Friends.Add(new Person {Name = "Friend 2", BirthDay = DateTime.UtcNow});
            person.Friends.Add(new Person {Name = "Friend 3", BirthDay = DateTime.UtcNow});

            return person;
        }

        public static void Main(string[] args)
        {
            var person = CreatePerson();

            // Make XML:
            var personSerialized = XmlSerializationUtils.Serialize(person);
            Console.WriteLine(personSerialized);
            Console.WriteLine();

            //
            // Make Object:
            var personDeserialized = XmlSerializationUtils.Deserialize<Person>(personSerialized);
            foreach (var friend in personDeserialized.Friends)
            {
                Console.WriteLine("{0} has a friend named {1}.",
                    personDeserialized.Name, friend.Name);
            }

            Console.WriteLine();
            Console.ReadKey();
        }
    }
}