Horizon – 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; |
||
6 | using System.Threading.Tasks; |
||
7 | using System.Xml; |
||
8 | using System.Xml.Schema; |
||
9 | using System.Xml.Serialization; |
||
10 | |||
11 | namespace Horizon.Utilities.Serialization |
||
12 | { |
||
13 | public static class Serialization |
||
14 | { |
||
15 | #region Public Methods |
||
16 | |||
17 | public static async Task<SerializationState> Deserialize<T>(string file, string targetNamespace, |
||
18 | string schemeUri, |
||
19 | CancellationToken cancellationToken) |
||
20 | { |
||
21 | var xmlSerializer = new XmlSerializer(typeof(T)); |
||
22 | |||
23 | var validationEventArgs = new List<ValidationEventArgs>(); |
||
24 | |||
25 | void XmlReaderSettingsValidationEventHandler(object sender, ValidationEventArgs e) |
||
26 | { |
||
27 | validationEventArgs.Add(e); |
||
28 | } |
||
29 | |||
30 | T servers; |
||
31 | |||
32 | var settings = new XmlReaderSettings(); |
||
33 | |||
34 | try |
||
35 | { |
||
36 | settings.Async = true; |
||
37 | settings.DtdProcessing = DtdProcessing.Parse; |
||
38 | settings.ValidationType = ValidationType.Schema; |
||
39 | |||
40 | settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | |
||
41 | XmlSchemaValidationFlags.ProcessSchemaLocation | |
||
42 | XmlSchemaValidationFlags.ReportValidationWarnings | |
||
43 | XmlSchemaValidationFlags.ProcessIdentityConstraints; |
||
44 | |||
45 | settings.Schemas = new XmlSchemaSet(); |
||
46 | |||
47 | settings.ValidationEventHandler += XmlReaderSettingsValidationEventHandler; |
||
48 | |||
49 | settings.Schemas.Add(targetNamespace, schemeUri); |
||
50 | |||
51 | using (var fileStream = |
||
52 | await Miscellaneous.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, |
||
53 | cancellationToken)) |
||
54 | { |
||
55 | using (var xmlReader = XmlReader.Create(fileStream, |
||
56 | settings)) |
||
57 | { |
||
58 | var stringBuilder = new StringBuilder(); |
||
59 | |||
60 | using (var stringWriter = new StringWriter(stringBuilder)) |
||
61 | { |
||
62 | while (await xmlReader.ReadAsync()) |
||
63 | { |
||
64 | await stringWriter.WriteAsync(await xmlReader.ReadOuterXmlAsync()); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | using (var stringReader = new StringReader(stringBuilder.ToString())) |
||
69 | { |
||
70 | servers = |
||
71 | (T)xmlSerializer |
||
72 | .Deserialize(stringReader); |
||
73 | } |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | catch (Exception exception) |
||
78 | { |
||
79 | return new SerializationFailure(exception, validationEventArgs); |
||
80 | } |
||
81 | finally |
||
82 | { |
||
83 | settings.ValidationEventHandler -= |
||
84 | XmlReaderSettingsValidationEventHandler; |
||
85 | } |
||
86 | |||
87 | return new SerializationSuccess<T>(servers, validationEventArgs); |
||
88 | } |
||
89 | |||
90 | public static async Task<SerializationState> Serialize<T>(T servers, string file, string name, string subset, |
||
91 | CancellationToken cancellationToken) |
||
92 | { |
||
93 | var xmlSerializer = new XmlSerializer(typeof(T)); |
||
94 | |||
95 | try |
||
96 | { |
||
97 | using (var memoryStream = new MemoryStream()) |
||
98 | { |
||
99 | using (var xmlWriter = |
||
100 | XmlWriter.Create(memoryStream, |
||
101 | new XmlWriterSettings |
||
102 | { |
||
103 | Async = true, |
||
104 | Indent = true, |
||
105 | IndentChars = " ", |
||
106 | OmitXmlDeclaration = false |
||
107 | })) |
||
108 | { |
||
109 | await xmlWriter.WriteDocTypeAsync(name, |
||
110 | null, |
||
111 | null, |
||
112 | subset); |
||
113 | |||
114 | xmlSerializer.Serialize(xmlWriter, servers); |
||
115 | |||
116 | using (var fileStream = |
||
117 | await Miscellaneous.GetFileStream(file, FileMode.Create, FileAccess.Write, |
||
118 | FileShare.Write, |
||
119 | cancellationToken)) |
||
120 | { |
||
121 | memoryStream.Position = 0L; |
||
122 | |||
123 | await memoryStream.CopyToAsync(fileStream); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | catch (Exception exception) |
||
129 | { |
||
130 | return new SerializationFailure(exception); |
||
131 | } |
||
132 | |||
133 | return new SerializationSuccess<T>(); |
||
134 | } |
||
135 | |||
136 | #endregion |
||
137 | } |
||
138 | } |