mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-02-14 05:36:30 +00:00
45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using System.IO;
|
|
using System.Xml;
|
|
using System.Xml.Schema;
|
|
using System.Xml.Serialization;
|
|
|
|
namespace SabreTools.Serialization.Streams
|
|
{
|
|
/// <summary>
|
|
/// Base class for other XML serializers
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public partial class XmlFile<T> : IStreamSerializer<T>
|
|
{
|
|
/// <inheritdoc/>
|
|
#if NET48
|
|
public T Deserialize(Stream data)
|
|
#else
|
|
public T? Deserialize(Stream? data)
|
|
#endif
|
|
{
|
|
// If the stream is null
|
|
if (data == null)
|
|
return default;
|
|
|
|
// Setup the serializer and the reader
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
var settings = new XmlReaderSettings
|
|
{
|
|
CheckCharacters = false,
|
|
DtdProcessing = DtdProcessing.Ignore,
|
|
ValidationFlags = XmlSchemaValidationFlags.None,
|
|
ValidationType = ValidationType.None,
|
|
};
|
|
var streamReader = new StreamReader(data);
|
|
var xmlReader = XmlReader.Create(streamReader, settings);
|
|
|
|
// Perform the deserialization and return
|
|
#if NET48
|
|
return (T)serializer.Deserialize(xmlReader);
|
|
#else
|
|
return (T?)serializer.Deserialize(xmlReader);
|
|
#endif
|
|
}
|
|
}
|
|
} |