view Implab/Xml/SerializationHelpers.cs @ 247:fb70574741a1 v3

working on promises
author cin
date Fri, 26 Jan 2018 18:46:27 +0300
parents 3eaa9372c754
children 7c7e9ad6fe4a
line wrap: on
line source

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>(T obj) {
            return SerializersPool<T>.Instance.SerializeAsString(obj);
        }

        public static void Serialize<T>(XmlWriter writer, T obj) {
            SerializersPool<T>.Instance.Serialize(writer, obj);
        }

        public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
            var doc = new XmlDocument();
            using (var writer = doc.CreateNavigator().AppendChild()) {
                SerializersPool<T>.Instance.Serialize(writer, obj);
            }
            return doc;
        }

        public static XDocument SerializeAsXDocument<T>(T obj) {
            var doc = new XDocument();
            using (var writer = doc.CreateWriter()) {
                SerializersPool<T>.Instance.Serialize(writer, obj);
            }
            return doc;
        }

        public static void SerializeToFile<T>(string file, T obj) {
            using (var writer = File.CreateText(file))
                SerializersPool<T>.Instance.Serialize(writer, obj);
        }

        public static T DeserializeFromString<T>(string data) {
            return SerializersPool<T>.Instance.DeserializeFromString(data);
        }

        public static T DeserializeFromXmlNode<T>(XmlNode node) {
            Safe.ArgumentNotNull(node, nameof(node));
            using (var reader = node.CreateNavigator().ReadSubtree())
                return SerializersPool<T>.Instance.Deserialize(reader);
        }
    }
}