mirror of
https://github.com/SabreTools/SabreTools.Serialization.git
synced 2026-02-13 21:33:38 +00:00
69 lines
2.4 KiB
C#
69 lines
2.4 KiB
C#
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
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>
|
|
{
|
|
/// <summary>
|
|
/// Serializes the defined type to a stream
|
|
/// </summary>
|
|
/// <param name="obj">Data to serialize</param>
|
|
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
|
#if NET48
|
|
public Stream Serialize(T obj)
|
|
#else
|
|
public Stream? Serialize(T? obj)
|
|
#endif
|
|
=> Serialize(obj, null, null, null, null);
|
|
|
|
/// <summary>
|
|
/// Serializes the defined type to a stream
|
|
/// </summary>
|
|
/// <param name="obj">Data to serialize</param>
|
|
/// <param name="name">Optional DOCTYPE name</param>
|
|
/// <param name="pubid">Optional DOCTYPE pubid</param>
|
|
/// <param name="sysid">Optional DOCTYPE sysid</param>
|
|
/// <param name="subset">Optional DOCTYPE name</param>
|
|
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
|
#if NET48
|
|
public Stream Serialize(T obj, string name = null, string pubid = null, string sysid = null, string subset = null)
|
|
#else
|
|
public Stream? Serialize(T? obj, string? name = null, string? pubid = null, string? sysid = null, string? subset = null)
|
|
#endif
|
|
{
|
|
// If the object is null
|
|
if (obj == null)
|
|
return null;
|
|
|
|
// Setup the serializer and the reader
|
|
var serializer = new XmlSerializer(typeof(T));
|
|
var settings = new XmlWriterSettings
|
|
{
|
|
CheckCharacters = false,
|
|
Encoding = Encoding.UTF8,
|
|
Indent = true,
|
|
IndentChars = "\t",
|
|
NewLineChars = "\n",
|
|
};
|
|
var stream = new MemoryStream();
|
|
var streamWriter = new StreamWriter(stream);
|
|
var xmlWriter = XmlWriter.Create(streamWriter, settings);
|
|
|
|
// Write the doctype if provided
|
|
if (!string.IsNullOrWhiteSpace(name))
|
|
xmlWriter.WriteDocType(name, pubid, sysid, subset);
|
|
|
|
// Perform the deserialization and return
|
|
serializer.Serialize(xmlWriter, obj);
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
return stream;
|
|
}
|
|
}
|
|
} |