Files
SongTidy/SongTidy/Services/PlanXmlIO.cs
T

106 lines
3.4 KiB
C#

using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using SongTidy.Models;
namespace SongTidy.Services;
/// <summary>
/// Reads and writes plan documents in the application's XML format.
/// </summary>
public static class PlanXmlIO {
#region Stream access
/// <summary>
/// Writes a plan to a stream as UTF-8 XML.
/// </summary>
/// <param name="plan">The plan to write.</param>
/// <param name="stream">The destination stream. The stream is left open.</param>
public static void Save(SongPlan plan, Stream stream) {
XmlSerializer serializer = new(typeof(SongPlan));
// Suppress xsi and xsd namespace attributes to keep the plan file compact.
XmlSerializerNamespaces namespaces = new();
namespaces.Add(string.Empty, string.Empty);
using XmlWriter writer = XmlWriter.Create(stream, CreateWriterSettings());
serializer.Serialize(writer, plan, namespaces);
}
/// <summary>
/// Reads a plan from a stream.
/// </summary>
/// <param name="stream">The source stream. The stream is left open.</param>
/// <returns>The deserialized plan.</returns>
/// <exception cref="InvalidDataException">The document does not contain a plan.</exception>
public static SongPlan Load(Stream stream) {
XmlSerializer serializer = new(typeof(SongPlan));
using XmlReader reader = XmlReader.Create(stream, CreateReaderSettings());
SongPlan? plan = (SongPlan?)serializer.Deserialize(reader);
if (plan is null) {
throw new InvalidDataException("The XML document does not contain a plan.");
}
return plan;
}
#endregion
#region File access
/// <summary>
/// Writes a plan to a UTF-8 XML file, overwriting an existing file.
/// </summary>
/// <param name="plan">The plan to write.</param>
/// <param name="filePath">The destination file path.</param>
public static void Save(SongPlan plan, string filePath) {
using FileStream stream = new(filePath, FileMode.Create, FileAccess.Write, FileShare.None);
Save(plan, stream);
}
/// <summary>
/// Reads a plan from an XML file.
/// </summary>
/// <param name="filePath">The source file path.</param>
/// <returns>The deserialized plan.</returns>
public static SongPlan Load(string filePath) {
using FileStream stream = new(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
return Load(stream);
}
#endregion
#region Helpers
/// <summary>
/// Creates the XML writer settings used for plan files.
/// </summary>
/// <returns>Writer settings that produce readable UTF-8 XML without a byte order mark.</returns>
private static XmlWriterSettings CreateWriterSettings() {
return new XmlWriterSettings {
Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false),
Indent = true,
IndentChars = " ",
CloseOutput = false,
};
}
/// <summary>
/// Creates the XML reader settings used for plan files.
/// </summary>
/// <returns>Reader settings that prohibit DTD processing and leave the input stream open.</returns>
private static XmlReaderSettings CreateReaderSettings() {
return new XmlReaderSettings {
DtdProcessing = DtdProcessing.Prohibit,
XmlResolver = null,
CloseInput = false,
};
}
#endregion
}