using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.ComponentModel;
public struct XdrTypeWrapper<T>
class XdrTypeWrapperTypeDictionary
static XdrTypeWrapperTypeDictionary instance;
static XdrTypeWrapperTypeDictionary() { instance = new XdrTypeWrapper<T>.XdrTypeWrapperTypeDictionary(); }
public static XdrTypeWrapperTypeDictionary Instance { get { return instance; } }
readonly Dictionary<Type, string> dict;
XdrTypeWrapperTypeDictionary()
dict = new Dictionary<Type, string>
{ typeof(string), "string" },
{ typeof(ushort), "u2" },
{ typeof(ulong), "ui8" },
{ typeof(DateTime), "dateTime" },
{ typeof(bool), "boolean" },
{ typeof(double), "float" },
public string DataType(Type type)
public XdrTypeWrapper(T value) { this.value = value; }
public static implicit operator XdrTypeWrapper<T>(T value) { return new XdrTypeWrapper<T>(value); }
public static implicit operator T(XdrTypeWrapper<T> wrapper) { return wrapper.Value; }
[XmlAttribute("dt", Namespace = "urn:schemas-microsoft-com:datatypes")]
return XdrTypeWrapperTypeDictionary.Instance.DataType(typeof(T));
public T Value { get { return value; } set { this.value = value; } }
public int Amount { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XdrTypeWrapper<int> XmlAmount { get { return Amount; } set { Amount = value; } }
public DateTime StartTime { get; set; }
[XmlElement("StartTime")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XdrTypeWrapper<DateTime> XmlStartTime { get { return StartTime; } set { StartTime = value; } }
public double Double { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XdrTypeWrapper<double> XmlDouble { get { return Double; } set { Double = value; } }
public static void Test()
var test1 = new TestClass { Amount = 101, StartTime = DateTime.Now, Double = 101.23 };
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("dt", "urn:schemas-microsoft-com:datatypes");
var xml = test1.GetXml(namespaces);
var test2 = xml.LoadFromXML<TestClass>();
if (!(xml == test2.GetXml(namespaces)))
Debug.Assert(xml == test2.GetXml(namespaces));
throw new InvalidOperationException("!(xml == test2.GetXml(namespaces))");
if (!(test2.Amount == test1.Amount))
Debug.Assert(test2.Amount == test1.Amount);
throw new InvalidOperationException("!(test2.Amount == test1.Amount)");
if (!(test2.StartTime == test1.StartTime))
Debug.Assert(test2.StartTime == test1.StartTime);
throw new InvalidOperationException("!(test2.StartTime == test1.StartTime");
if (!(test2.Double == test1.Double))
Debug.Assert(test2.Double == test1.Double);
throw new InvalidOperationException("!(test2.Double == test1.Double)");
public static class XmlSerializationHelper
public static T LoadFromXML<T>(this string xmlString, XmlSerializer serializer = null)
T returnValue = default(T);
using (StringReader reader = new StringReader(xmlString))
object result = (serializer ?? new XmlSerializer(typeof(T))).Deserialize(reader);
public static string GetXml<T>(this T obj, XmlSerializerNamespaces ns = null, XmlWriterSettings settings = null, XmlSerializer serializer = null)
using (var textWriter = new StringWriter())
settings = settings ?? new XmlWriterSettings() { Indent = true, IndentChars = " " };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
(serializer ?? new XmlSerializer(typeof(T))).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
public static void Main()