comparison 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
comparison
equal deleted inserted replaced
277:963b17c275be 278:6691aff01de1
1 using System.IO;
2 using System.Text;
3 using System.Xml;
4 using System.Xml.Linq;
5 using System.Xml.Serialization;
6
7 namespace Implab.Xml {
8 public static class XmlSerializerExtensions {
9
10 public static void Serialize(this XmlSerializer that, object obj, XmlElement element) {
11 Safe.ArgumentNotNull(that, nameof(that));
12 using (var writer = element.CreateNavigator().AppendChild())
13 that.Serialize(writer, obj);
14 }
15
16 public static void Serialize(this XmlSerializer that, object obj, XElement element) {
17 Safe.ArgumentNotNull(that, nameof(that));
18 using (var writer = element.CreateWriter())
19 that.Serialize(writer, obj);
20 }
21
22 public static XDocument SerializeAsXDocumnet(this XmlSerializer that, object obj) {
23 Safe.ArgumentNotNull(that, nameof(that));
24 var doc = new XDocument();
25 using (var writer = doc.CreateWriter()) {
26 that.Serialize(writer, obj);
27 }
28 return doc;
29 }
30
31 public static XmlDocument SerializeAsXmlDocument(this XmlSerializer that, object obj) {
32 Safe.ArgumentNotNull(that, nameof(that));
33 var doc = new XmlDocument();
34 using (var writer = doc.CreateNavigator().AppendChild()) {
35 that.Serialize(writer, obj);
36 }
37 return doc;
38 }
39 public static string SerializeAsString(this XmlSerializer that, object obj) {
40 Safe.ArgumentNotNull(that, nameof(that));
41 using (var writer = new StringWriter()) {
42 that.Serialize(writer, obj);
43 return writer.ToString();
44 }
45 }
46
47 public static void SerializeToFile(this XmlSerializer that, object obj, string file, Encoding encoding) {
48 Safe.ArgumentNotNull(that, nameof(that));
49 using (var writer = new StreamWriter(File.Create(file),encoding))
50 that.Serialize(writer, obj);
51 }
52
53 public static void SerializeToFile(this XmlSerializer that, object obj, string file) {
54 SerializeToFile(that, obj, file, Encoding.UTF8);
55 }
56
57 public static object Deserialize(this XmlSerializer that, XmlElement element) {
58 Safe.ArgumentNotNull(that, nameof(that));
59 Safe.ArgumentNotNull(element, nameof(element));
60
61 using (var reader = element.CreateNavigator().ReadSubtree())
62 return that.Deserialize(reader);
63 }
64
65 public static object Deserialize(this XmlSerializer that, XElement element) {
66 Safe.ArgumentNotNull(that, nameof(that));
67 Safe.ArgumentNotNull(element, nameof(element));
68
69 using (var reader = element.CreateReader())
70 return that.Deserialize(reader);
71 }
72
73 public static object DeserializeFromString(this XmlSerializer that, string text) {
74 Safe.ArgumentNotNull(that, nameof(that));
75
76 using(var reader = new StringReader(text))
77 return that.Deserialize(reader);
78 }
79
80 public static object DeserializeFromFile(this XmlSerializer that, string file) {
81 Safe.ArgumentNotNull(that, nameof(that));
82
83 using(var reader = File.OpenRead(file))
84 return that.Deserialize(reader);
85 }
86
87
88 }
89 }