mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-04-06 06:11:45 +00:00
This change looks dramatic, but it's just separating out the already-split namespaces into separate top-level folders. In theory, every single one could be built into their own Nuget package. `SabreTools.Serialization` still builds the normal Nuget package that is used by all other projects and includes all namespaces.
48 lines
1.4 KiB
C#
48 lines
1.4 KiB
C#
using System.IO;
|
|
using System.Xml;
|
|
using System.Xml.Schema;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace SabreTools.Serialization.Readers
|
|
{
|
|
/// <summary>
|
|
/// Base class for other XML deserializers
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public abstract class XmlFile<T> : BaseBinaryReader<T>
|
|
{
|
|
/// <inheritdoc/>
|
|
public override T? Deserialize(Stream? data)
|
|
{
|
|
// If the stream is invalid
|
|
if (data is null || !data.CanRead)
|
|
return default;
|
|
|
|
try
|
|
{
|
|
// Setup the serializer and the reader
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
var settings = new XmlReaderSettings
|
|
{
|
|
CheckCharacters = false,
|
|
#if NET40_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
|
|
DtdProcessing = DtdProcessing.Ignore,
|
|
#endif
|
|
ValidationFlags = XmlSchemaValidationFlags.None,
|
|
ValidationType = ValidationType.None,
|
|
};
|
|
var streamReader = new StreamReader(data);
|
|
var xmlReader = XmlReader.Create(streamReader, settings);
|
|
|
|
// Perform the deserialization and return
|
|
return (T?)serializer.Deserialize(xmlReader);
|
|
}
|
|
catch
|
|
{
|
|
// Ignore the actual error
|
|
return default;
|
|
}
|
|
}
|
|
}
|
|
}
|