CSharp · Console
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Xml.Linq;
[XmlRoot(ElementName = "customer")]
public class Customer
{
bool? isSimpleFormat;
[XmlIgnore]
public bool IsSimpleFormat { get { return isSimpleFormat == true; } set { isSimpleFormat = value; } }
[XmlIgnore]
bool IsSimpleFormatDefined { get { return isSimpleFormat != null; } }
[XmlIgnore]
bool IsComplexPropertySpecified
{
get
{
return !IsSimpleFormat;
}
set
{
if (IsSimpleFormatDefined && IsSimpleFormat)
{
throw new InvalidOperationException("Cannot set a complex property on a simple customer");
}
IsSimpleFormat = !value;
}
}
[XmlElement(ElementName = "number")]
public string Number { get; set; }
[XmlIgnore]
public bool NumberSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
[XmlElement(ElementName = "internalId")]
public string InternalId { get; set; }
[XmlIgnore]
public bool InternalIdSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
[XmlElement(ElementName = "lastModifiedDateTime")]
public string LastModifiedDateTime { get; set; }
[XmlIgnore]
public bool LastModifiedDateTimeSpecified { get { return IsComplexPropertySpecified; } set { IsComplexPropertySpecified = value; } }
[XmlText]
public string SimpleNumber
{
get
{
return IsSimpleFormat ? Number : null;
}
set
{
IsComplexPropertySpecified = false;
Number = value;
}
}
}
[XmlRoot(ElementName = "SalesOrder")]
public class SalesOrder
{
[XmlElement(ElementName = "orderType")]
public string OrderType { get; set; }
[XmlElement(ElementName = "customer")]
public Customer Customer { get; set; }
}
class TestClass
{
static string GetSimplifiedXml()
{
var xml = @"<SalesOrder>
<orderType>SO</orderType>
<customer>10000</customer>
</SalesOrder>";
return xml;
}
static string GetFullXml()
{
var xml = @"<SalesOrder>
<orderType>SO</orderType>
<customer>
<number>10000</number>
<internalId>2462</internalId>
<lastModifiedDateTime>0001-01-01</lastModifiedDateTime>
</customer>
</SalesOrder>";
return xml;
}
static string GetInvalidXml()
{
var xml = @"<SalesOrder>
<orderType>SO</orderType>
<customer>10000<number>10000</number>
<internalId>2462</internalId>
<lastModifiedDateTime>0001-01-01</lastModifiedDateTime>
</customer>
</SalesOrder>";
return xml;
}
public static void Test()
{
Test(GetSimplifiedXml());
Test(GetFullXml());
try
{
Test(GetInvalidXml());
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: ");
Console.WriteLine(xml);
var salesOrder = xml.LoadFromXML<SalesOrder>();
var xml2 = salesOrder.GetXml(true);
Console.WriteLine("Reserialized XML: ");
Console.WriteLine(xml2);
if (!XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)))
{
throw new InvalidOperationException("");
}
else
{
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));
Console.WriteLine();
}
}
public class Program
{
public static void Main()
{
TestClass.Test();
}
}
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();
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))
new XmlSerializer(obj.GetType()).Serialize(xmlWriter, obj, ns);
return textWriter.ToString();
}
}
}