using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.Serialization; using System.Xml; namespace Pariveda.DataAccess35.Business { /// /// Utility class for serialization of LINQ Entities using WCF Serialization /// public static class SerializerUtil { /// /// Serializes the specified entity. /// /// The entity to serialize /// public static string Serialize(this T entity) { StringWriter writer = new StringWriter(); XmlTextWriter xmlWriter = new XmlTextWriter(writer); DataContractSerializer serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(xmlWriter, entity); return writer.ToString(); } /// /// Deserializes the specified XML to the specified type /// /// Type to deserialize to /// The XML of the object graph /// public static T Deserialize(string xml) { DataContractSerializer serializer = new DataContractSerializer(typeof(T)); StringReader reader = new StringReader(xml); XmlTextReader xmlReader = new XmlTextReader(reader); return (T)serializer.ReadObject(xmlReader); } /// /// Clones the specified customer by serializing and deserializing the entity /// /// The entity to clone /// public static T Clone(this T entity) { return Deserialize(Serialize(entity)); } } }