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
|
229
|
41 public static T DeserializeFromString<T>(string data) {
|
|
42 return SerializersPool<T>.Instance.DeserializeFromString(data);
|
|
43 }
|
|
44
|
|
45 public static T DeserializeFromXmlNode<T>(XmlNode node) {
|
|
46 Safe.ArgumentNotNull(node, nameof(node));
|
|
47 using (var reader = node.CreateNavigator().ReadSubtree())
|
|
48 return SerializersPool<T>.Instance.Deserialize(reader);
|
|
49 }
|
|
50 }
|
|
51 }
|