Files
SabreTools.Serialization/SabreTools.Serialization.Readers/XmlFile.cs
Matt Nadareski 7689c6dd07 Libraries
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.
2026-03-21 16:26:56 -04:00

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;
}
}
}
}