misu – Blame information for rev 1
?pathlinks?
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 Configuration |
||
9 | { |
||
10 | public static class Serialization |
||
11 | { |
||
12 | #region Private Delegates, Events, Enums, Properties, Indexers and Fields |
||
13 | |||
14 | private static XmlSerializer ConfigurationSerializer { get; } = new XmlSerializer(typeof(Settings)); |
||
15 | |||
16 | private static XmlReaderSettings XmlReaderSettings { get; } = new XmlReaderSettings |
||
17 | { |
||
18 | Async = true, |
||
19 | ValidationType = ValidationType.Schema, |
||
20 | IgnoreWhitespace = true, |
||
21 | ValidationFlags = |
||
22 | XmlSchemaValidationFlags |
||
23 | .ProcessInlineSchema | |
||
24 | XmlSchemaValidationFlags |
||
25 | .ProcessSchemaLocation | |
||
26 | XmlSchemaValidationFlags |
||
27 | .ReportValidationWarnings, |
||
28 | DtdProcessing = DtdProcessing.Parse |
||
29 | }; |
||
30 | |||
31 | private static XmlWriterSettings XmlWriterSettings { get; } = new XmlWriterSettings |
||
32 | { |
||
33 | Async = true, |
||
34 | Indent = true, |
||
35 | IndentChars = " ", |
||
36 | OmitXmlDeclaration = false |
||
37 | }; |
||
38 | |||
39 | #endregion |
||
40 | |||
41 | #region Public Methods |
||
42 | |||
43 | public static async Task<SerializationState> Serialize(this Settings settings, string file) |
||
44 | { |
||
45 | try |
||
46 | { |
||
47 | using (var streamWriter = new StreamWriter(file)) |
||
48 | { |
||
49 | using (var xmlWriter = |
||
50 | XmlWriter.Create(streamWriter, XmlWriterSettings)) |
||
51 | { |
||
52 | await xmlWriter.WriteDocTypeAsync("Configuration", |
||
53 | null, |
||
54 | null, |
||
55 | "<!ATTLIST Configuration xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
56 | |||
57 | ConfigurationSerializer.Serialize(xmlWriter, settings); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | return new SerializationSuccess(); |
||
62 | } |
||
63 | catch (Exception ex) |
||
64 | { |
||
65 | return new SerializationFailure {Exception = ex}; |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public static SerializationState Deserialize(string file) |
||
70 | { |
||
71 | try |
||
72 | { |
||
73 | Settings configuration; |
||
74 | |||
75 | using (var streamReader = new StreamReader(file)) |
||
76 | { |
||
77 | using (var xmlReader = XmlReader.Create(streamReader, XmlReaderSettings)) |
||
78 | { |
||
79 | configuration = (Settings) ConfigurationSerializer.Deserialize(xmlReader); |
||
80 | } |
||
81 | } |
||
82 | |||
83 | return new SerializationSuccess<Settings> |
||
84 | { |
||
85 | Result = configuration |
||
86 | }; |
||
87 | } |
||
88 | catch (Exception ex) |
||
89 | { |
||
90 | return new SerializationFailure |
||
91 | { |
||
92 | Exception = ex |
||
93 | }; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | #endregion |
||
98 | } |
||
99 | } |