Common
A library of common classes.
XmlSerialization.cs
1using System.IO;
2using System.Runtime.Serialization;
3using System.Text;
4using System.Xml;
5
7{
11 public static class XmlSerialization
12 {
16 public static XmlWriterSettings PrettyXmlWriterSettings()
17 {
18 return new XmlWriterSettings
19 {
20 Indent = true,
21 IndentChars = "\t",
22 NewLineChars = "\n",
23 };
24 }
25
31 public static string GetXmlString<T>(T graph, XmlWriterSettings settings = null)
32 {
33 var serializer = new DataContractSerializer(typeof(T));
34 settings = settings ?? PrettyXmlWriterSettings();
35
36 using (var stream = new MemoryStream())
37 {
38 using (var writer = XmlWriter.Create(stream, settings))
39 {
40 serializer.WriteObject(writer, graph);
41 }
42
43 stream.Seek(0, SeekOrigin.Begin);
44
45 using (var reader = new StreamReader(stream))
46 {
47 return reader.ReadToEnd();
48 }
49 }
50 }
51
57 public static void SaveXml<T>(string path, T graph)
58 {
59 var serializer = new DataContractSerializer(typeof(T));
60
61 using (var stream = File.Create(path))
62 {
63 serializer.WriteObject(stream, graph);
64 }
65 }
66
71 public static T LoadXml<T>(string path)
72 {
73 var serializer = new DataContractSerializer(typeof(T));
74
75 using (var stream = File.OpenRead(path))
76 {
77 return (T)serializer.ReadObject(stream);
78 }
79 }
80
85 public static T LoadXml<T>(byte[] bytes)
86 {
87 var serializer = new DataContractSerializer(typeof(T));
88
89 using (var stream = new MemoryStream(bytes))
90 {
91 return (T)serializer.ReadObject(stream);
92 }
93 }
94
99 public static T LoadXmlString<T>(string xml)
100 {
101 return LoadXml<T>(Encoding.UTF8.GetBytes(xml));
102 }
103
110 public static void SaveEncryptedXml<T>(string path, T graph, byte[] key)
111 {
112 var serializer = new DataContractSerializer(typeof(T));
113
114 using (var stream = File.Create(path))
115 {
116 Cryptography.EncryptToStream(stream, serializer, graph, key);
117 }
118 }
119
125 public static T LoadEncryptedXml<T>(string path, byte[] key)
126 {
127 var serializer = new DataContractSerializer(typeof(T));
128
129 using (var stream = File.OpenRead(path))
130 {
131 return Cryptography.DecryptFromStream<T>(stream, serializer, key);
132 }
133 }
134 }
135}
Contains methods related to encryption and decryption.
Definition: Cryptography.cs:11
Contains methods for serializing objects to and from XML.
static void SaveXml< T >(string path, T graph)
Saves the object to the file path using the DataContractSerializer.
static XmlWriterSettings PrettyXmlWriterSettings()
Returns a new instance of XML writer settings for pretty printing.
static T LoadXmlString< T >(string xml)
Loads an object from an XML string using the DataContractSerializer.
static void SaveEncryptedXml< T >(string path, T graph, byte[] key)
Serializes and encrypts and object to the specified file.
static string GetXmlString< T >(T graph, XmlWriterSettings settings=null)
Returns the pretty XML string for the object.
static T LoadEncryptedXml< T >(string path, byte[] key)
Decrypts and deserializes an object from the specified file.
static T LoadXml< T >(string path)
Loads an object from a file path using the DataContractSerializer.