using System.Collections.Generic;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.ComponentModel;
this.InnerItem = new Inner();
public Inner InnerItem { get; set; }
[XmlElement(typeof(Inner))]
[XmlElement(typeof(object))]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public object InnerItemXmlProxy
InnerItem = (Inner)value;
public static void Main()
var outer = new Outer { InnerItem = new Inner() };
var xml = outer.GetXml();
Console.WriteLine("Serialized XML:");
var outer2 = xml.LoadFromXML<Outer>();
var xml2 = outer2.GetXml();
Console.WriteLine("Re-serialized XML:");
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();
using (var textWriter = new StringWriter())
var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();