Winify – Blame information for rev

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