##// END OF EJS Templates
Added SerializationHelpers.SerializeToFile method
cin -
r231:3eaa9372c754 v2
parent child
Show More
@@ -1,45 +1,51
1 using System;
1 using System;
2 using System.Collections.Generic;
2 using System.Collections.Generic;
3 using System.IO;
3 using System.Linq;
4 using System.Linq;
4 using System.Text;
5 using System.Text;
5 using System.Threading.Tasks;
6 using System.Threading.Tasks;
6 using System.Xml;
7 using System.Xml;
7 using System.Xml.Linq;
8 using System.Xml.Linq;
8
9
9 namespace Implab.Xml {
10 namespace Implab.Xml {
10 public static class SerializationHelpers {
11 public static class SerializationHelpers {
11 public static string SerializeAsString<T>(T obj) {
12 public static string SerializeAsString<T>(T obj) {
12 return SerializersPool<T>.Instance.SerializeAsString(obj);
13 return SerializersPool<T>.Instance.SerializeAsString(obj);
13 }
14 }
14
15
15 public static void Serialize<T>(XmlWriter writer, T obj) {
16 public static void Serialize<T>(XmlWriter writer, T obj) {
16 SerializersPool<T>.Instance.Serialize(writer, obj);
17 SerializersPool<T>.Instance.Serialize(writer, obj);
17 }
18 }
18
19
19 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
20 public static XmlDocument SerializeAsXmlDocument<T>(T obj) {
20 var doc = new XmlDocument();
21 var doc = new XmlDocument();
21 using (var writer = doc.CreateNavigator().AppendChild()) {
22 using (var writer = doc.CreateNavigator().AppendChild()) {
22 SerializersPool<T>.Instance.Serialize(writer, obj);
23 SerializersPool<T>.Instance.Serialize(writer, obj);
23 }
24 }
24 return doc;
25 return doc;
25 }
26 }
26
27
27 public static XDocument SerializeAsXDocument<T>(T obj) {
28 public static XDocument SerializeAsXDocument<T>(T obj) {
28 var doc = new XDocument();
29 var doc = new XDocument();
29 using (var writer = doc.CreateWriter()) {
30 using (var writer = doc.CreateWriter()) {
30 SerializersPool<T>.Instance.Serialize(writer, obj);
31 SerializersPool<T>.Instance.Serialize(writer, obj);
31 }
32 }
32 return doc;
33 return doc;
33 }
34 }
34
35
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
35 public static T DeserializeFromString<T>(string data) {
41 public static T DeserializeFromString<T>(string data) {
36 return SerializersPool<T>.Instance.DeserializeFromString(data);
42 return SerializersPool<T>.Instance.DeserializeFromString(data);
37 }
43 }
38
44
39 public static T DeserializeFromXmlNode<T>(XmlNode node) {
45 public static T DeserializeFromXmlNode<T>(XmlNode node) {
40 Safe.ArgumentNotNull(node, nameof(node));
46 Safe.ArgumentNotNull(node, nameof(node));
41 using (var reader = node.CreateNavigator().ReadSubtree())
47 using (var reader = node.CreateNavigator().ReadSubtree())
42 return SerializersPool<T>.Instance.Deserialize(reader);
48 return SerializersPool<T>.Instance.Deserialize(reader);
43 }
49 }
44 }
50 }
45 }
51 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now