Question41235191 c-sharp-desalinizing-xml-xmltext-gets-null-instead-of-string-itself

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

    // https://stackoverflow.com/questions/41235191/c-sharp-desalinizing-xml-xmltext-gets-null-instead-of-string-itself

    public class SnellMessage
    {
    }

    [XmlType("Instance_Updated"), Serializable]
    public class InstanceUpdated : SnellMessage
    {
        public InstanceUpdated()
        {
            InstanceFields = new List<Field>()
            {
                new Field() { Name = "INSTANCE_LABEL" },
                new Field() { Name = "MAT_ID" },
                new Field() { Name = "DEVICE_ID" }
            };

            ItemFields = new List<Field>()
            {
                new Field() { Name = "MATERIAL_ID" },
                new Field() { Name = "TITLE" },
                new Field() { Name = "ON_AIR_DURATION" }
            };
        }

        [XmlArray("Instance")]
        [XmlArrayItem("Field")]
        public List<Field> InstanceFields { get; set; }

        [XmlArray("Item")]
        [XmlArrayItem("Field")]
        public List<Field> ItemFields { get; set; }
    }

    [XmlType("Field"), Serializable]
    public class Field
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlText]
        public string Value { get; set; }
    }

    class TestClass
    {
        static string GetXml()
        {
            var xml = @"<Instance_Updated>
  <Instance>
    <Field Name=""INSTANCE_LABEL"">00010541</Field>
    <Field Name=""MAT_ID"">TEST</Field>
    <Field Name=""DEVICE_ID"">TEST</Field>
  </Instance>
  <Item>
    <Field Name=""MATERIAL_ID"">TEST</Field>
    <Field Name=""TITLE"">TEST</Field>
    <Field Name=""ON_AIR_DURATION"">A</Field>
  </Item>
</Instance_Updated>";
            return xml;
        }

        internal static void Test()
        {
            var xml = GetXml();

            var instanceUpdated = xml.LoadFromXml<InstanceUpdated>();

            var xml2 = instanceUpdated.GetXml(true);

			Console.WriteLine("Deserialized and re-serialized {0}:", instanceUpdated);
            Console.WriteLine(xml2);
        }
    }

	public class Program
	{
		public static void Main()
		{
			TestClass.Test();
		}
	}

    public static class XmlSerializationHelper
    {
        public static T LoadFromXml<T>(this string xmlString)
        {
            using (StringReader reader = new StringReader(xmlString))
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(reader);
            }
        }

        public static string GetXml<T>(this T obj, 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))
                    new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
                return textWriter.ToString();
            }
        }
    }
;