Spring – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.IO;
3 using System.Threading.Tasks;
4 using System.Xml;
5 using System.Xml.Schema;
6 using System.Xml.Serialization;
7  
8 namespace Spring.Serialization.Configuration
9 {
10 public static class ConfigurationSerialization
11 {
12 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
13  
14 private static XmlSerializer ConfigurationSerializer { get; } =
15 new XmlSerializer(typeof(global::Configuration.Configuration));
16  
17 private static XmlReaderSettings XmlReaderSettings { get; } = new XmlReaderSettings
18 {
19 Async = true,
20 ValidationType = ValidationType.Schema,
21 IgnoreWhitespace = true,
22 ValidationFlags =
23 XmlSchemaValidationFlags
24 .ProcessInlineSchema |
25 XmlSchemaValidationFlags
26 .ProcessSchemaLocation |
27 XmlSchemaValidationFlags
28 .ReportValidationWarnings,
29 DtdProcessing = DtdProcessing.Parse
30 };
31  
32 private static XmlWriterSettings XmlWriterSettings { get; } = new XmlWriterSettings
33 {
34 Async = true,
35 Indent = true,
36 IndentChars = " ",
37 OmitXmlDeclaration = false
38 };
39  
40 #endregion
41  
42 #region Public Methods
43  
44 public static async Task<SerializationState> Serialize(this global::Configuration.Configuration configuration,
45 string file)
46 {
47 try
48 {
49 using (var streamWriter = new StreamWriter(file))
50 {
51 using (var xmlWriter =
52 XmlWriter.Create(streamWriter, XmlWriterSettings))
53 {
54 await xmlWriter.WriteDocTypeAsync("Configuration",
55 null,
56 null,
57 "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>");
58  
59 ConfigurationSerializer.Serialize(xmlWriter, configuration);
60 }
61 }
62  
63 return new SerializationSuccess();
64 }
65 catch (Exception ex)
66 {
67 return new SerializationFailure { Exception = ex };
68 }
69 }
70  
71 public static SerializationState Deserialize(FileInfo fileInfo)
72 {
73 try
74 {
75 global::Configuration.Configuration configuration;
76  
77 using (var streamReader = new StreamReader(fileInfo.FullName))
78 {
79 using (var xmlReader = XmlReader.Create(streamReader, XmlReaderSettings))
80 {
81 configuration =
82 (global::Configuration.Configuration) ConfigurationSerializer.Deserialize(xmlReader);
83 }
84 }
85  
86 return new SerializationSuccess<global::Configuration.Configuration>
87 {
88 Result = configuration
89 };
90 }
91 catch (Exception ex)
92 {
93 return new SerializationFailure
94 {
95 Exception = ex
96 };
97 }
98 }
99  
100 public static SerializationState Deserialize(byte[] data)
101 {
102 try
103 {
104 global::Configuration.Configuration configuration;
105  
106 using (var streamReader = new MemoryStream(data))
107 {
108 using (var xmlReader = XmlReader.Create(streamReader, XmlReaderSettings))
109 {
110 configuration =
111 (global::Configuration.Configuration) ConfigurationSerializer.Deserialize(xmlReader);
112 }
113 }
114  
115 return new SerializationSuccess<global::Configuration.Configuration>
116 {
117 Result = configuration
118 };
119 }
120 catch (Exception ex)
121 {
122 return new SerializationFailure
123 {
124 Exception = ex
125 };
126 }
127 }
128  
129 #endregion
130 }
131 }