questions/49977144/c-sharp-xml-deserialization-of-1-xml-to-different-classes

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;

    //https://stackoverflow.com/questions/49977144/c-sharp-xml-deserialization-of-1-xml-to-different-classes#comment87012845_49977144

    public class Project
    {
        // Name for your root element.  Replace as desired.
        public const string RootElementName = "Project";
        // Namespace for your project.  Replace as required.
        public const string RootElementNamespaceURI = "https://stackoverflow.com/questions/49977144";

        public string BaseProperty { get; set; }
    }

    public class ProjectCustomerA : Project
    {
        public string CustomerProperty { get; set; }

        public string ProjectCustomerAProperty { get; set; }
    }

    public class ProjectCustomerB : Project
    {
        public string CustomerProperty { get; set; }

        public string ProjectCustomerBProperty { get; set; }
    }

    public static partial class XmlSerializationHelper
    {
        public static T LoadFromXmlAsType<T>(this string xmlString)
        {
            return new StringReader(xmlString).LoadFromXmlAsType<T>();
        }

        public static T LoadFromXmlAsType<T>(this TextReader textReader)
        {
            using (var xmlReader = XmlReader.Create(textReader, new XmlReaderSettings { CloseInput = false }))
                return xmlReader.LoadFromXmlAsType<T>();
        }

        public static T LoadFromXmlAsType<T>(this XmlReader xmlReader)
        {
            while (xmlReader.NodeType != XmlNodeType.Element)
                if (!xmlReader.Read())
                    throw new XmlException("No root element");
            var serializer = XmlSerializerFactory.Create(typeof(T), xmlReader.LocalName, xmlReader.NamespaceURI);
            return (T)serializer.Deserialize(xmlReader);
        }

        public static string SaveToXmlAsType<T>(this T obj, string localName, string namespaceURI)
        {
            var sb = new StringBuilder();
            using (var writer = new StringWriter(sb))
                obj.SaveToXmlAsType(writer, localName, namespaceURI);
            return sb.ToString();
        }

        public static void SaveToXmlAsType<T>(this T obj, TextWriter textWriter, string localName, string namespaceURI)
        {
            using (var xmlWriter = XmlWriter.Create(textWriter, new XmlWriterSettings { CloseOutput = false, Indent = true }))
                obj.SaveToXmlAsType(xmlWriter, localName, namespaceURI);
        }

        public static void SaveToXmlAsType<T>(this T obj, XmlWriter xmlWriter, string localName, string namespaceURI)
        {
            var serializer = XmlSerializerFactory.Create(obj.GetType(), localName, namespaceURI);
            serializer.Serialize(xmlWriter, obj);
        }
    }

    public static class XmlSerializerFactory
    {
        // To avoid a memory leak the serializer must be cached.
        // https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer
        // This factory taken from 
        // https://stackoverflow.com/questions/34128757/wrap-properties-with-cdata-section-xml-serialization-c-sharp/34138648#34138648

        readonly static Dictionary<Tuple<Type, string, string>, XmlSerializer> cache;
        readonly static object padlock;

        static XmlSerializerFactory()
        {
            padlock = new object();
            cache = new Dictionary<Tuple<Type, string, string>, XmlSerializer>();
        }

        public static XmlSerializer Create(Type serializedType, string rootName, string rootNamespace)
        {
            if (serializedType == null)
                throw new ArgumentNullException();
            if (rootName == null && rootNamespace == null)
                return new XmlSerializer(serializedType);
            lock (padlock)
            {
                XmlSerializer serializer;
                var key = Tuple.Create(serializedType, rootName, rootNamespace);
                if (!cache.TryGetValue(key, out serializer))
                    cache[key] = serializer = new XmlSerializer(serializedType, new XmlRootAttribute { ElementName = rootName, Namespace = rootNamespace });
                return serializer;
            }
        }
    }

    class TestClass
    {
        public static void Test()
        {
            var roota = new ProjectCustomerA
            {
                BaseProperty = "base property value",
                CustomerProperty = "shared property value",
                ProjectCustomerAProperty = "project A value",
            };
            var xmla = roota.SaveToXmlAsType(Project.RootElementName, Project.RootElementNamespaceURI);

            var rootb = xmla.LoadFromXmlAsType<ProjectCustomerB>();
            var xmlb = rootb.SaveToXmlAsType(Project.RootElementName, Project.RootElementNamespaceURI);
			
            // Assert that the shared BaseProperty was deserialized successfully.
            Assert.IsTrue(roota.BaseProperty == rootb.BaseProperty);
            // Assert that the same-named CustomerProperty was ported over properly.
            Assert.IsTrue(roota.CustomerProperty == rootb.CustomerProperty);
			
            Console.WriteLine("\nXML for {0}:", roota);
            Console.WriteLine(xmla);
			
            Console.WriteLine("\nDeserialized and re-serialized XML for {0}:", rootb);
            Console.WriteLine(xmlb);
        }
    }
				
    public class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
						
	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
			Console.WriteLine();

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