Mercurial > pub > ImplabNet
diff Implab/Xml/XmlSerializerExtensions.cs @ 278:6691aff01de1 v3
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
Implab.ServiceHost: rewritten TypeReference (added support for nested types), stable API
author | cin |
---|---|
date | Thu, 03 May 2018 09:59:44 +0300 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Xml/XmlSerializerExtensions.cs Thu May 03 09:59:44 2018 +0300 @@ -0,0 +1,89 @@ +using System.IO; +using System.Text; +using System.Xml; +using System.Xml.Linq; +using System.Xml.Serialization; + +namespace Implab.Xml { + public static class XmlSerializerExtensions { + + public static void Serialize(this XmlSerializer that, object obj, XmlElement element) { + Safe.ArgumentNotNull(that, nameof(that)); + using (var writer = element.CreateNavigator().AppendChild()) + that.Serialize(writer, obj); + } + + public static void Serialize(this XmlSerializer that, object obj, XElement element) { + Safe.ArgumentNotNull(that, nameof(that)); + using (var writer = element.CreateWriter()) + that.Serialize(writer, obj); + } + + public static XDocument SerializeAsXDocumnet(this XmlSerializer that, object obj) { + Safe.ArgumentNotNull(that, nameof(that)); + var doc = new XDocument(); + using (var writer = doc.CreateWriter()) { + that.Serialize(writer, obj); + } + return doc; + } + + public static XmlDocument SerializeAsXmlDocument(this XmlSerializer that, object obj) { + Safe.ArgumentNotNull(that, nameof(that)); + var doc = new XmlDocument(); + using (var writer = doc.CreateNavigator().AppendChild()) { + that.Serialize(writer, obj); + } + return doc; + } + public static string SerializeAsString(this XmlSerializer that, object obj) { + Safe.ArgumentNotNull(that, nameof(that)); + using (var writer = new StringWriter()) { + that.Serialize(writer, obj); + return writer.ToString(); + } + } + + public static void SerializeToFile(this XmlSerializer that, object obj, string file, Encoding encoding) { + Safe.ArgumentNotNull(that, nameof(that)); + using (var writer = new StreamWriter(File.Create(file),encoding)) + that.Serialize(writer, obj); + } + + public static void SerializeToFile(this XmlSerializer that, object obj, string file) { + SerializeToFile(that, obj, file, Encoding.UTF8); + } + + public static object Deserialize(this XmlSerializer that, XmlElement element) { + Safe.ArgumentNotNull(that, nameof(that)); + Safe.ArgumentNotNull(element, nameof(element)); + + using (var reader = element.CreateNavigator().ReadSubtree()) + return that.Deserialize(reader); + } + + public static object Deserialize(this XmlSerializer that, XElement element) { + Safe.ArgumentNotNull(that, nameof(that)); + Safe.ArgumentNotNull(element, nameof(element)); + + using (var reader = element.CreateReader()) + return that.Deserialize(reader); + } + + public static object DeserializeFromString(this XmlSerializer that, string text) { + Safe.ArgumentNotNull(that, nameof(that)); + + using(var reader = new StringReader(text)) + return that.Deserialize(reader); + } + + public static object DeserializeFromFile(this XmlSerializer that, string file) { + Safe.ArgumentNotNull(that, nameof(that)); + + using(var reader = File.OpenRead(file)) + return that.Deserialize(reader); + } + + + } +} \ No newline at end of file