questions/49740709/xmlarray-with-multiple-names

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.ComponentModel;
using System.Diagnostics;

//https://stackoverflow.com/questions/49740709/xmlarray-with-multiple-names?noredirect=1

	public class root 
	{
		[XmlArray("largeImages")]
		[XmlArrayItem("largeImage")]
		public List<image> Images { get; set; } = new List<image>();
	
		[XmlArray("smallImages")]
		[XmlArrayItem("smallImage")]
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
		public List<image> SmallImagesSurrogate { get { return Images; } }
		
		public bool ShouldSerializeSmallImagesSurrogate() { return false; }
	}

	public class image
	{
		[XmlElement(ElementName="url")]
		public string Url { get; set; }
		[XmlElement(ElementName="height")]
		public string Height { get; set; }
		[XmlElement(ElementName="width")]
		public string Width { get; set; }
	}

    public class TestClass
    {
        public static void Test()
        {
			var xml = GetXml();
			
			var root = xml.LoadFromXml<root>();
			
			var xml2 = root.GetXml(true);
			
			Console.WriteLine("Re-serialized {0}:", root);
			Console.WriteLine(xml2);
			
			Assert.IsTrue(XElement.Parse(xml).Descendants("url").Count() == root.Images.Count);
			Assert.IsTrue(XElement.Parse(xml).Descendants("url").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Url)));
			Assert.IsTrue(XElement.Parse(xml).Descendants("width").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Width)));
			Assert.IsTrue(XElement.Parse(xml).Descendants("height").Select(e => (string)e).SequenceEqual(root.Images.Select(i => i.Height)));
			
			Console.WriteLine("All tests passed.");
        }
		
		static string GetXml()
		{
			var xml = @"<root>
<smallImages>
    <smallImage>
        <url>./imageSmall.jpg</url>
        <height>240</height>
        <width>320</width>
    </smallImage>
    <smallImage>
        <url>./imageSmall2.jpg</url>
        <height>240</height>
        <width>320</width>
    </smallImage>
</smallImages>
<largeImages>
    <largeImage>
        <url>./imageLarge.jpg</url>
        <height>480</height>
        <width>640</width>
    </largeImage>
    <largeImage>
        <url>./imageLarge2.jpg</url>
        <height>480</height>
        <width>640</width>
    </largeImage>
</largeImages>
<smallImages>
    <smallImage>
        <url>./imageSmall3.jpg</url>
        <height>240</height>
        <width>320</width>
    </smallImage>
</smallImages>
</root>";
			return xml;
			
		}
    }

    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 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;
			}
		}
	}
;