Widow – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.IO; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Xml; |
||
7 | using System.Xml.Schema; |
||
8 | using System.Xml.Serialization; |
||
9 | |||
10 | namespace Widow.Serialization |
||
11 | { |
||
12 | public static class WindowsSerialization |
||
13 | { |
||
14 | #region Public Enums, Properties and Fields |
||
15 | |||
16 | /// <summary> |
||
17 | /// Cached XML serializer for the configuration. |
||
18 | /// </summary> |
||
19 | [XmlIgnore] |
||
20 | public static XmlSerializer XmlSerializer { get; } = new XmlSerializer(typeof(Windows.Windows)); |
||
21 | |||
22 | #endregion |
||
23 | |||
24 | #region Public Methods |
||
25 | |||
26 | public static async Task<SerializationState> Deserialize(string file) |
||
27 | { |
||
28 | var validationEventArgs = new List<ValidationEventArgs>(); |
||
29 | |||
30 | void XmlReaderSettingsValidationEventHandler(object sender, ValidationEventArgs e) |
||
31 | { |
||
32 | validationEventArgs.Add(e); |
||
33 | } |
||
34 | |||
35 | Windows.Windows windows; |
||
36 | |||
37 | var settings = new XmlReaderSettings(); |
||
38 | |||
39 | try |
||
40 | { |
||
41 | settings.Async = true; |
||
42 | settings.DtdProcessing = DtdProcessing.Parse; |
||
43 | settings.ValidationType = ValidationType.Schema; |
||
44 | |||
45 | settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | |
||
46 | XmlSchemaValidationFlags.ProcessSchemaLocation | |
||
47 | XmlSchemaValidationFlags.ReportValidationWarnings | |
||
48 | XmlSchemaValidationFlags.ProcessIdentityConstraints; |
||
49 | |||
50 | settings.Schemas = new XmlSchemaSet(); |
||
51 | |||
52 | settings.ValidationEventHandler += XmlReaderSettingsValidationEventHandler; |
||
53 | |||
54 | settings.Schemas.Add("urn:widow-windows-schema", "Windows.xsd"); |
||
55 | |||
56 | using (var fileStream = |
||
57 | new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) |
||
58 | { |
||
59 | using (var xmlReader = XmlReader.Create(fileStream, |
||
60 | settings)) |
||
61 | { |
||
62 | var stringBuilder = new StringBuilder(); |
||
63 | |||
64 | using (var stringWriter = new StringWriter(stringBuilder)) |
||
65 | { |
||
66 | while (await xmlReader.ReadAsync()) |
||
67 | { |
||
68 | await stringWriter.WriteAsync(await xmlReader.ReadOuterXmlAsync()); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | using (var stringReader = new StringReader(stringBuilder.ToString())) |
||
73 | { |
||
74 | windows = |
||
75 | (Windows.Windows) XmlSerializer |
||
76 | .Deserialize(stringReader); |
||
77 | } |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | catch (Exception ex) |
||
82 | { |
||
83 | return new SerializationFailure(ex, validationEventArgs); |
||
84 | } |
||
85 | finally |
||
86 | { |
||
87 | settings.ValidationEventHandler -= |
||
88 | XmlReaderSettingsValidationEventHandler; |
||
89 | } |
||
90 | |||
91 | return new SerializationSuccess(windows, validationEventArgs); |
||
92 | } |
||
93 | |||
94 | public static async Task<SerializationState> Serialize(Windows.Windows windows, string file) |
||
95 | { |
||
96 | try |
||
97 | { |
||
98 | using (var memoryStream = new MemoryStream()) |
||
99 | { |
||
100 | using (var xmlWriter = |
||
101 | XmlWriter.Create(memoryStream, |
||
102 | new XmlWriterSettings |
||
103 | { |
||
104 | Async = true, |
||
105 | Indent = true, |
||
106 | IndentChars = " ", |
||
107 | OmitXmlDeclaration = false |
||
108 | })) |
||
109 | { |
||
110 | await xmlWriter.WriteDocTypeAsync("Windows", |
||
111 | null, |
||
112 | null, |
||
113 | "<!ATTLIST Windows xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
114 | |||
115 | XmlSerializer.Serialize(xmlWriter, windows); |
||
116 | |||
117 | using (var fileStream = |
||
118 | new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) |
||
119 | { |
||
120 | memoryStream.Position = 0L; |
||
121 | |||
122 | await memoryStream.CopyToAsync(fileStream); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | catch (Exception ex) |
||
128 | { |
||
129 | return new SerializationFailure(ex); |
||
130 | } |
||
131 | |||
132 | return new SerializationSuccess(); |
||
133 | } |
||
134 | |||
135 | public static async Task<SerializationState> Serialize(Windows.Windows windows, |
||
136 | MemoryStream outputStream) |
||
137 | { |
||
138 | try |
||
139 | { |
||
140 | using (var memoryStream = new MemoryStream()) |
||
141 | { |
||
142 | using (var xmlWriter = |
||
143 | XmlWriter.Create(memoryStream, |
||
144 | new XmlWriterSettings |
||
145 | { |
||
146 | Async = true, |
||
147 | Indent = true, |
||
148 | IndentChars = " ", |
||
149 | OmitXmlDeclaration = false |
||
150 | })) |
||
151 | { |
||
152 | await xmlWriter.WriteDocTypeAsync("Windows", |
||
153 | null, |
||
154 | null, |
||
155 | "<!ATTLIST Windows xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>"); |
||
156 | |||
157 | XmlSerializer.Serialize(xmlWriter, windows); |
||
158 | |||
159 | memoryStream.Position = 0L; |
||
160 | |||
161 | await memoryStream.CopyToAsync(outputStream); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | catch (Exception ex) |
||
166 | { |
||
167 | return new SerializationFailure(ex); |
||
168 | } |
||
169 | |||
170 | return new SerializationSuccess(); |
||
171 | } |
||
172 | |||
173 | #endregion |
||
174 | } |
||
175 | } |