Files
SabreTools.Serialization/SabreTools.Serialization.Readers/XmlFile.cs

48 lines
1.4 KiB
C#
Raw Normal View History

using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
2025-09-26 14:57:20 -04:00
namespace SabreTools.Serialization.Readers
2023-09-08 17:03:31 -04:00
{
/// <summary>
/// Base class for other XML deserializers
2023-09-08 17:03:31 -04:00
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class XmlFile<T> : BaseBinaryReader<T>
2023-09-08 17:03:31 -04:00
{
/// <inheritdoc/>
public override T? Deserialize(Stream? data)
{
// If the stream is invalid
2026-01-25 14:30:18 -05:00
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,
2025-07-24 09:19:38 -04:00
#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;
}
2023-09-08 17:03:31 -04:00
}
}
}