annotate Implab/Xml/XmlSerializerExtensions.cs @ 281:e0916ddc9950 v3 tip

code cleanup and refactoring
author cin
date Fri, 01 Jun 2018 21:35:24 +0300
parents 6691aff01de1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
278
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
1 using System.IO;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
2 using System.Text;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
3 using System.Xml;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
4 using System.Xml.Linq;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
5 using System.Xml.Serialization;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
6
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
7 namespace Implab.Xml {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
8 public static class XmlSerializerExtensions {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
9
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
10 public static void Serialize(this XmlSerializer that, object obj, XmlElement element) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
11 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
12 using (var writer = element.CreateNavigator().AppendChild())
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
13 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
14 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
15
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
16 public static void Serialize(this XmlSerializer that, object obj, XElement element) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
17 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
18 using (var writer = element.CreateWriter())
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
19 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
20 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
21
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
22 public static XDocument SerializeAsXDocumnet(this XmlSerializer that, object obj) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
23 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
24 var doc = new XDocument();
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
25 using (var writer = doc.CreateWriter()) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
26 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
27 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
28 return doc;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
29 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
30
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
31 public static XmlDocument SerializeAsXmlDocument(this XmlSerializer that, object obj) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
32 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
33 var doc = new XmlDocument();
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
34 using (var writer = doc.CreateNavigator().AppendChild()) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
35 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
36 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
37 return doc;
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
38 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
39 public static string SerializeAsString(this XmlSerializer that, object obj) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
40 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
41 using (var writer = new StringWriter()) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
42 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
43 return writer.ToString();
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
44 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
45 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
46
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
47 public static void SerializeToFile(this XmlSerializer that, object obj, string file, Encoding encoding) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
48 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
49 using (var writer = new StreamWriter(File.Create(file),encoding))
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
50 that.Serialize(writer, obj);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
51 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
52
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
53 public static void SerializeToFile(this XmlSerializer that, object obj, string file) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
54 SerializeToFile(that, obj, file, Encoding.UTF8);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
55 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
56
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
57 public static object Deserialize(this XmlSerializer that, XmlElement element) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
58 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
59 Safe.ArgumentNotNull(element, nameof(element));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
60
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
61 using (var reader = element.CreateNavigator().ReadSubtree())
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
62 return that.Deserialize(reader);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
63 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
64
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
65 public static object Deserialize(this XmlSerializer that, XElement element) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
66 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
67 Safe.ArgumentNotNull(element, nameof(element));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
68
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
69 using (var reader = element.CreateReader())
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
70 return that.Deserialize(reader);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
71 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
72
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
73 public static object DeserializeFromString(this XmlSerializer that, string text) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
74 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
75
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
76 using(var reader = new StringReader(text))
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
77 return that.Deserialize(reader);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
78 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
79
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
80 public static object DeserializeFromFile(this XmlSerializer that, string file) {
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
81 Safe.ArgumentNotNull(that, nameof(that));
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
82
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
83 using(var reader = File.OpenRead(file))
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
84 return that.Deserialize(reader);
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
85 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
86
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
87
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
88 }
6691aff01de1 Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)
cin
parents:
diff changeset
89 }