using System.Collections.Generic;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Diagnostics;
namespace System.Data.Services.Client
public class StoreItem<TEntity>
where TEntity : class, new()
public System.Data.Services.Client.EntityStates Status { get; set; }
public TEntity Entity { get; set; }
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
public XElement XmlEntity
return (Entity == null ? null : XObjectExtensions.SerializeToXElement(Entity, null, true));
Entity = (value == null ? null : XObjectExtensions.Deserialize<TEntity>(value));
public string AnAddedProperty { get; set; }
public static class XObjectExtensions
public static T Deserialize<T>(this XContainer element)
return element.Deserialize<T>(null);
public static T Deserialize<T>(this XContainer element, XmlSerializer serializer)
using (var reader = element.CreateReader())
serializer = serializer ?? new XmlSerializer(typeof(T));
object result = serializer.Deserialize(reader);
public static XElement SerializeToXElement<T>(this T obj)
return obj.SerializeToXElement(null, true);
public static XElement SerializeToXElement<T>(this T obj, XmlSerializer serializer, bool omitStandardNamespaces)
var doc = new XDocument();
using (var writer = doc.CreateWriter())
XmlSerializerNamespaces ns = null;
if (omitStandardNamespaces)
(ns = new XmlSerializerNamespaces()).Add("", "");
serializer = serializer ?? new XmlSerializer(obj.GetType());
serializer.Serialize(writer, obj, ns);
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)
return obj.GetXml(false);
public static string GetXml<T>(this T obj, bool omitStandardNamespaces)
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();
public class LandscapeArea
public double Area { get; set; }
public double Area { get; set; }
public class AreaWrapper<TEntity> where TEntity : class, new()
public TEntity Item { get; set; }
public static void Test()
Test(new SewageArea { Area = 10101 });
Test(new LandscapeArea { Area = 202 });
Test(new List<string> { "a", "b", "c" });
Test(new AreaWrapper<SewageArea> { Item = new SewageArea { Area = 202 } });
Test(new List<List<string>> { new List<string> { "a", "b", "c" }});
static void Test<TEntity>(TEntity entity) where TEntity : class, new()
var test = new StoreItem<TEntity> { Entity = entity, AnAddedProperty = "some value of " + typeof(TEntity).Name, Status = System.Data.Services.Client.EntityStates.State1 };
var xml = test.GetXml(true);
Console.WriteLine("Serialized XML for " + typeof(TEntity).FullName + ": ");
var test2 = xml.LoadFromXML<StoreItem<TEntity>>();
var xml2 = test2.GetXml(true);
Console.WriteLine("Deserialized and re-serialized XML: ");
if (XNode.DeepEquals(XElement.Parse(xml), XElement.Parse(xml2)))
Console.WriteLine("Initial and re-serializd XML are equivalent.");
throw new InvalidOperationException("Initial and re-serializd XML are NOT equivalent.");
public static void Main()