Files
SabreTools.Serialization/Streams/XmlFile.Deserializer.cs

40 lines
1.2 KiB
C#
Raw Permalink Normal View History

2023-09-08 17:03:31 -04:00
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using SabreTools.Serialization.Interfaces;
2023-09-08 17:03:31 -04:00
namespace SabreTools.Serialization.Streams
{
/// <summary>
/// Base class for other XML serializers
/// </summary>
/// <typeparam name="T"></typeparam>
public partial class XmlFile<T> : IStreamSerializer<T>
{
/// <inheritdoc/>
public T? Deserialize(Stream? data)
{
// 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,
2023-11-21 20:59:20 -05:00
#if NET40_OR_GREATER || NETCOREAPP
2023-09-08 17:03:31 -04:00
DtdProcessing = DtdProcessing.Ignore,
2023-11-21 20:59:20 -05:00
#endif
2023-09-08 17:03:31 -04:00
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);
}
}
}