using System.Collections.Generic;
using System.Xml.Serialization;
[XmlRoot(ElementName = "customer")]
public bool IsSimpleFormat { get { return isSimpleFormat == true; } set { isSimpleFormat = value; } }
bool IsSimpleFormatDefined { get { return isSimpleFormat != null; } }
bool IsComplexPropertySpecified
if (IsSimpleFormatDefined && IsSimpleFormat)
throw new InvalidOperationException("Cannot set a complex property on a simple customer");
[XmlElement(ElementName = "number")]
public string Number { get; set; }
public bool NumberSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
[XmlElement(ElementName = "internalId")]
public string InternalId { get; set; }
public bool InternalIdSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
[XmlElement(ElementName = "lastModifiedDateTime")]
public string LastModifiedDateTime { get; set; }
public bool LastModifiedDateTimeSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
public string SimpleNumber
return IsSimpleFormat ? Number : null;
IsComplexPropertySpecified = false;
[XmlRoot(ElementName = "SalesOrder")]
[XmlElement(ElementName = "orderType")]
public string OrderType { get; set; }
[XmlElement(ElementName = "customer")]
public Customer Customer { get; set; }
static string GetSimplifiedXml()
<orderType>SO</orderType>
<customer>10000</customer>
static string GetFullXml()
<orderType>SO</orderType>
<internalId>2462</internalId>
<lastModifiedDateTime>0001-01-01</lastModifiedDateTime>
static string GetInvalidXml()
<orderType>SO</orderType>
<customer>10000<number>10000</number>
<internalId>2462</internalId>
<lastModifiedDateTime>0001-01-01</lastModifiedDateTime>
public static void Test()
Test(GetSimplifiedXml());
throw new ApplicationException("Failed to throw InvalidOperationException");
catch (InvalidOperationException ex)
Console.WriteLine("Caught required exception: " + ex.Message);
static void Test(string xml)
Console.WriteLine("Original XML: ");
var salesOrder = xml.LoadFromXML<SalesOrder>();
var xml2 = salesOrder.GetXml(true);
Console.WriteLine("Reserialized XML: ");
if (!XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)))
throw new InvalidOperationException("");
Console.WriteLine("Original and re-serialized XML are equivalent.");
salesOrder.Customer.IsSimpleFormat = true;
Console.WriteLine("Re-serialized XML in simple format: ");
Console.WriteLine(salesOrder.GetXml(true));
public static void Main()
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 };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();