[Fork] /questions/52720996/c-sharp-xml-serializable-enum-type

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

//https://stackoverflow.com/questions/52720996/c-sharp-xml-serializable-enum-type

	[Serializable]
	public enum Res_Type
	{
		[XmlEnum(Name = "Integer")]
		Integer,
		[XmlEnum(Name = "Decimal")]
		Decimal,
		[XmlEnum(Name = "Text")]
		Text
	}

	public class Ressource
	{
	   [XmlAttribute]
	   public string name { set; get; }
	   [XmlAttribute]
	   public Res_Type type { get; set; }
	}
		
    public class TestClass
    {
        public static void Test()
        {
            Ressource res = new Ressource();
            res.name = "ressource_name";
            res.type = Res_Type.Integer;

			var fileName = @"test.xml";
			
            XmlSerializer serializer = new XmlSerializer(res.GetType());
            using (StreamWriter writer = new StreamWriter(fileName))
            {
                serializer.Serialize(writer, res);
            }

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Ressource));
			using (var reader = new StreamReader(fileName))
			{
				res = (Ressource)xmlSerializer.Deserialize(reader);
			}			

			Console.WriteLine(File.ReadAllText(fileName));
			
			Assert.IsTrue(res.type ==  Res_Type.Integer && res.name == "ressource_name");
        }
    }

    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("Roslyn 2.0 Compiler; Environment version: " + Environment.Version);
			Console.WriteLine();

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