Winify

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 7  →  ?path2? @ 8
/trunk/Winify/Servers/Serialization/ServersSerialization.cs
@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
 
namespace Winify.Servers.Serialization
{
public static class ServersSerialization
{
#region Public Enums, Properties and Fields
 
/// <summary>
/// Cached XML serializer for the configuration.
/// </summary>
public static XmlSerializer XmlSerializer { get; } = new XmlSerializer(typeof(global::Servers.Servers));
 
#endregion
 
#region Public Methods
 
public static async Task<ServersSerializationState> Deserialize(string file)
{
var validationEventArgs = new List<ValidationEventArgs>();
 
void XmlReaderSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
validationEventArgs.Add(e);
}
 
global::Servers.Servers servers;
 
var settings = new XmlReaderSettings();
 
try
{
settings.Async = true;
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.Schema;
 
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation |
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints;
 
settings.Schemas = new XmlSchemaSet();
 
settings.ValidationEventHandler += XmlReaderSettingsValidationEventHandler;
 
settings.Schemas.Add("urn:winify-servers-schema", "Servers.xsd");
 
using (var fileStream =
new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (var xmlReader = XmlReader.Create(fileStream,
settings))
{
var stringBuilder = new StringBuilder();
 
using (var stringWriter = new StringWriter(stringBuilder))
{
while (await xmlReader.ReadAsync())
{
await stringWriter.WriteAsync(await xmlReader.ReadOuterXmlAsync());
}
}
 
using (var stringReader = new StringReader(stringBuilder.ToString()))
{
servers =
(global::Servers.Servers) XmlSerializer
.Deserialize(stringReader);
}
}
}
}
catch (Exception ex)
{
return new ServersSerializationFailure(ex, validationEventArgs);
}
finally
{
settings.ValidationEventHandler -=
XmlReaderSettingsValidationEventHandler;
}
 
return new ServersSerializationSuccess(servers, validationEventArgs);
}
 
public static async Task<ServersSerializationState> Serialize(global::Servers.Servers servers, string file)
{
try
{
using (var memoryStream = new MemoryStream())
{
using (var xmlWriter =
XmlWriter.Create(memoryStream,
new XmlWriterSettings
{
Async = true,
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = false
}))
{
await xmlWriter.WriteDocTypeAsync("Servers",
null,
null,
"<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>");
 
XmlSerializer.Serialize(xmlWriter, servers);
 
using (var fileStream =
new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
memoryStream.Position = 0L;
 
await memoryStream.CopyToAsync(fileStream);
}
}
}
}
catch (Exception ex)
{
return new ServersSerializationFailure(ex);
}
 
return new ServersSerializationSuccess();
}
 
public static async Task<ServersSerializationState> Serialize(global::Servers.Servers servers,
MemoryStream outputStream)
{
try
{
using (var memoryStream = new MemoryStream())
{
using (var xmlWriter =
XmlWriter.Create(memoryStream,
new XmlWriterSettings
{
Async = true,
Indent = true,
IndentChars = " ",
OmitXmlDeclaration = false
}))
{
await xmlWriter.WriteDocTypeAsync("Servers",
null,
null,
"<!ATTLIST Servers xmlns:xsi CDATA #IMPLIED xsi:noNamespaceSchemaLocation CDATA #IMPLIED>");
 
XmlSerializer.Serialize(xmlWriter, servers);
 
memoryStream.Position = 0L;
 
await memoryStream.CopyToAsync(outputStream);
}
}
}
catch (Exception ex)
{
return new ServersSerializationFailure(ex);
}
 
return new ServersSerializationSuccess();
}
 
#endregion
}
}