using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; namespace Implab.Xml { public static class SerializationHelpers { public static string SerializeAsString(T obj) { return SerializersPool.Instance.SerializeAsString(obj); } public static void Serialize(XmlWriter writer, T obj) { SerializersPool.Instance.Serialize(writer, obj); } public static XmlDocument SerializeAsXmlDocument(T obj) { var doc = new XmlDocument(); using (var writer = doc.CreateNavigator().AppendChild()) { SerializersPool.Instance.Serialize(writer, obj); } return doc; } public static XDocument SerializeAsXDocument(T obj) { var doc = new XDocument(); using (var writer = doc.CreateWriter()) { SerializersPool.Instance.Serialize(writer, obj); } return doc; } public static void SerializeToFile(string file, T obj) { using (var writer = File.CreateText(file)) SerializersPool.Instance.Serialize(writer, obj); } public static void SerializeToElementChild(XmlElement element, T obj) { using(var writer = element.CreateNavigator().AppendChild()) SerializersPool.Instance.Serialize(writer, obj); } public static T Deserialize(XmlReader reader) { return SerializersPool.Instance.Deserialize(reader); } public static T DeserializeFromFile(string file) { using(var reader = XmlReader.Create(File.OpenText(file))) return Deserialize(reader); } public static T DeserializeFromString(string data) { return SerializersPool.Instance.DeserializeFromString(data); } public static T DeserializeFromXmlNode(XmlNode node) { Safe.ArgumentNotNull(node, nameof(node)); using (var reader = node.CreateNavigator().ReadSubtree()) return SerializersPool.Instance.Deserialize(reader); } } }