Question32883177

CSharp · Console

/Program.cs

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

using RestSharp.Deserializers;
using RestSharp.Extensions;
using RestSharp;
using System.Diagnostics;

    public class Setting
    {
        public int Cmd { get; set; }
        public int Status { get; set; }
    }

    [XmlRoot("Function")]
    public class MyResponse
    {
        [XmlIgnore]
        public List<Setting> Settings { get; set; }

        /// <summary>
        /// Proxy property to convert Settings to an alternating sequence of Cmd / Status elements.
        /// </summary>
        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [XmlAnyElement]
        public XElement[] Elements 
        {
            get
            {
                if (Settings == null)
                    return null;
                return Settings.SelectMany(s => new[] { new XElement("Cmd", s.Cmd), new XElement("Status", s.Status) }).ToArray();
            }
            set
            {
                if (value == null)
                    Settings = null;
                else
                    Settings = value.Where(e => e.Name == "Cmd").Zip(value.Where(e => e.Name == "Status"), (cmd, status) => new Setting { Cmd = (int)cmd, Status = (int)status }).ToList();
            }
        }
    }

    public class TestClass
    {
        public static string GetXml()
        {
            string xml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
<Function>
    <Cmd>2002</Cmd>
    <Status>1</Status>
    <Cmd>2003</Cmd>
    <Status>0</Status>
    <Cmd>2004</Cmd>
    <Status>0</Status>
    <Cmd>1012</Cmd>
    <Status>3</Status>
    <Cmd>2006</Cmd>
    <Status>0</Status>
    <Cmd>2007</Cmd>
    <Status>0</Status>
</Function>";
            return xml;
        }

        public static void Test()
        {
            RestSharp.RestResponse response = new RestSharp.RestResponse();
            response.Content = GetXml();

            var serializer = new DotNetXmlDeserializer();
            var myResponse = serializer.Deserialize<MyResponse>(response);

            if (!(myResponse.Settings.Count == XElement.Parse(response.Content).Elements("Cmd").Count()))
            {
                Debug.Assert(myResponse.Settings.Count == XElement.Parse(response.Content).Elements("Cmd").Count());
                throw new InvalidOperationException("Debug.Assert(myResponse.Settings.Count == XElement.Parse(response.Content).Elements(\"Cmd\").Count());");
            }
			
			Console.WriteLine(myResponse.GetXml());
        }
    }

    public static class XmlExtensions
    {
        public static string GetXml<T>(this T obj, XmlSerializerNamespaces ns = null, bool? standalone = null, XmlSerializer serializer = null)
        {
            serializer = serializer ?? new XmlSerializer(obj.GetType());
            using (var textWriter = new Utf8StringWriter())
            {
                var settings = new XmlWriterSettings() { Indent = true, IndentChars = "    " }; // For cosmetic purposes.
                using (var xmlWriter = XmlWriter.Create(textWriter, settings))
                {
                    if (standalone != null)
                        xmlWriter.WriteStartDocument(standalone.Value);
                    serializer.Serialize(xmlWriter, obj, ns);
                }
                return textWriter.ToString();
            }
        }		
    }

    public class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding
        {
            get { return Encoding.UTF8; }
        }
    }

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