// // using System; using System.IO; using System.Runtime.Serialization; using System.Xml.Serialization; namespace Deltares.Standard { /// /// Types that implement this interface has a seperate data object /// that needs to be serialized instead of the implementer itself /// /// /// The type implementing this interface act as an adapter without subclassing (Decorator pattern). /// It can have a complete different data contract interface and can delegate calls to the members /// of the object being serialized /// /// public interface ISerializableObject { /// /// Should return the data object to serialize /// /// T GetDataObject(); /// /// Sets the data object which needs to be serialized later /// Data properties in the implementer should implemenent setters and getters on this object /// /// void SetDataObject(T obj); } /// /// A common exception which should be trown when validating the schema and mandatory fields are not set. /// [Serializable] public class NotSchemaCompliantException : Exception { public NotSchemaCompliantException() { } public NotSchemaCompliantException(string message) : base(message) { } public NotSchemaCompliantException(string message, Exception inner) : base(message, inner) { } protected NotSchemaCompliantException( SerializationInfo info, StreamingContext context) : base(info, context) { } } /// /// Defines a contract for the Serializer. /// public interface INamespaceProvider { /// /// Defines a method which should return the target namespaces /// /// The qualified name XmlSerializerNamespaces GetNamespaces(); } public static class Serialization { /// /// Serializes the target to file /// /// The target type that implements ISchemaSerializer /// The target instance /// The name of the file to serialze to public static void SerializeToFile(T target, string fileName) { XmlSerializerNamespaces namespaces = null; var ns = target as INamespaceProvider; if (ns != null) namespaces = ns.GetNamespaces(); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Create)) { if (namespaces != null) xml.Serialize(fs, target, namespaces); else xml.Serialize(fs, target); } } /// /// Serializes the target to string /// /// The target type /// The target instance /// the target serialized as a xml string public static string SerializeToString(T target) { string result = ""; XmlSerializerNamespaces namespaces = null; var ns = target as INamespaceProvider; if (ns != null) namespaces = ns.GetNamespaces(); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var sw = new StringWriter()) { if (namespaces != null) xml.Serialize(sw, target, namespaces); else xml.Serialize(sw, target); result = sw.ToString(); } return result; } /// /// Deserializes the target object class from file /// /// The target type that implements ISchemaSerializer /// The name of the file to deserialize from /// The target object graph public static T DeserializeFromFile(string fileName) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Open)) { result = xml.Deserialize(fs) as T; } return result; } /// /// Deserializes the target object class from file /// /// The target type that implements ISchemaSerializer /// The name of the file to deserialize from /// The target object graph public static string DeserializeFromFileToString(string fileName) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var fs = new FileStream(fileName, FileMode.Open)) { result = xml.Deserialize(fs) as T; } return SerializeToString(result); } /// /// Deserializes the target object from string /// /// /// /// public static T DeserializeFromString(string xmlData) where T : class { T result = default(T); var xml = new System.Xml.Serialization.XmlSerializer(typeof (T)); using (var sr = new StringReader(xmlData)) { result = xml.Deserialize(sr) as T; } return result; } } }