2024-04-03 20:55:02 -04:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using System.Xml.Schema;
|
|
|
|
|
using System.Xml.Serialization;
|
2023-09-15 22:34:47 -04:00
|
|
|
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-08 17:03:31 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2024-04-03 17:27:08 -04:00
|
|
|
/// Base class for other XML deserializers
|
2023-09-08 17:03:31 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2025-09-26 15:02:43 -04:00
|
|
|
public abstract class XmlFile<T> : BaseBinaryReader<T>
|
2023-09-08 17:03:31 -04:00
|
|
|
{
|
2024-04-03 20:55:02 -04:00
|
|
|
/// <inheritdoc/>
|
2024-04-04 01:55:05 -04:00
|
|
|
public override T? Deserialize(Stream? data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-27 12:43:52 -05:00
|
|
|
// If the stream is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2024-04-03 20:55:02 -04:00
|
|
|
return default;
|
|
|
|
|
|
2024-11-27 12:43:52 -05:00
|
|
|
try
|
|
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// 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
|
2024-11-28 22:57:12 -05:00
|
|
|
DtdProcessing = DtdProcessing.Ignore,
|
2024-04-03 20:55:02 -04:00
|
|
|
#endif
|
2024-11-28 22:57:12 -05:00
|
|
|
ValidationFlags = XmlSchemaValidationFlags.None,
|
|
|
|
|
ValidationType = ValidationType.None,
|
|
|
|
|
};
|
|
|
|
|
var streamReader = new StreamReader(data);
|
|
|
|
|
var xmlReader = XmlReader.Create(streamReader, settings);
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Perform the deserialization and return
|
2024-11-27 12:43:52 -05:00
|
|
|
return (T?)serializer.Deserialize(xmlReader);
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Ignore the actual error
|
2024-11-27 12:43:52 -05:00
|
|
|
return default;
|
|
|
|
|
}
|
2023-09-08 17:03:31 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|