229
|
1 using System;
|
|
2 using System.Collections.Generic;
|
231
|
3 using System.IO;
|
229
|
4 using System.Linq;
|
|
5 using System.Text;
|
|
6 using System.Threading.Tasks;
|
|
7 using System.Xml;
|
|
8 using System.Xml.Linq;
|
|
9
|
|
10 namespace Implab.Xml {
|
|
11 public static class SerializationHelpers {
|
|
12 public static string SerializeAsString<T>(T obj) {
|
|
13 return SerializersPool<T>.Instance.SerializeAsString(obj);
|
|
14 }
|
|
15
|
|
16 public static void Serialize<T>(XmlWriter writer, T obj) {
|
|
17 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
18 }
|
|
19
|
|
20 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
|
|
21 var doc = new XmlDocument();
|
|
22 using (var writer = doc.CreateNavigator().AppendChild()) {
|
|
23 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
24 }
|
|
25 return doc;
|
|
26 }
|
|
27
|
|
28 public static XDocument SerializeAsXDocument<T>(T obj) {
|
|
29 var doc = new XDocument();
|
|
30 using (var writer = doc.CreateWriter()) {
|
|
31 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
32 }
|
|
33 return doc;
|
|
34 }
|
|
35
|
231
|
36 public static void SerializeToFile<T>(string file, T obj) {
|
|
37 using (var writer = File.CreateText(file))
|
|
38 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
39 }
|
|
40
|
251
|
41 public static void SerializeToElementChild<T>(XmlElement element, T obj) {
|
|
42 using(var writer = element.CreateNavigator().AppendChild())
|
|
43 SerializersPool<T>.Instance.Serialize(writer, obj);
|
|
44 }
|
|
45
|
|
46 public static T Deserialize<T>(XmlReader reader) {
|
|
47 return SerializersPool<T>.Instance.Deserialize(reader);
|
|
48 }
|
|
49
|
|
50 public static T DeserializeFromFile<T>(string file) {
|
|
51 using(var reader = XmlReader.Create(File.OpenText(file)))
|
|
52 return Deserialize<T>(reader);
|
|
53 }
|
|
54
|
229
|
55 public static T DeserializeFromString<T>(string data) {
|
|
56 return SerializersPool<T>.Instance.DeserializeFromString(data);
|
|
57 }
|
|
58
|
|
59 public static T DeserializeFromXmlNode<T>(XmlNode node) {
|
|
60 Safe.ArgumentNotNull(node, nameof(node));
|
|
61 using (var reader = node.CreateNavigator().ReadSubtree())
|
|
62 return SerializersPool<T>.Instance.Deserialize(reader);
|
|
63 }
|
|
64 }
|
|
65 }
|