5using System.Xml.Serialization;
48 encoderShouldEmitUTF8Identifier:
false);
89 bool validateBeforeSerialization =
true,
90 bool validateCurrentFingerprint =
true,
93 ArgumentNullException.ThrowIfNull(instance);
95 if (
string.IsNullOrWhiteSpace(filePath))
97 throw new ArgumentException(
98 "A destination file path is required.",
102 string fullFilePath =
106 string? directoryPath =
107 Path.GetDirectoryName(
110 if (!
string.IsNullOrWhiteSpace(directoryPath) &&
111 !Directory.Exists(directoryPath))
113 throw new DirectoryNotFoundException(
114 $
"The destination directory " +
115 $
"'{directoryPath}' does not exist.");
118 string temporaryFilePath =
121 Guid.NewGuid().ToString(
"N") +
140 validateBeforeSerialization:
141 validateBeforeSerialization,
143 validateCurrentFingerprint:
144 validateCurrentFingerprint,
160 if (File.Exists(temporaryFilePath))
209 bool validateBeforeSerialization =
true,
210 bool validateCurrentFingerprint =
true,
213 ArgumentNullException.ThrowIfNull(instance);
214 ArgumentNullException.ThrowIfNull(stream);
216 if (!stream.CanWrite)
218 throw new ArgumentException(
219 "The destination stream must be writable.",
223 if (validateBeforeSerialization)
229 validateCurrentFingerprint:
230 validateCurrentFingerprint);
233 XmlWriterSettings writerSettings =
234 CreateWriterSettings(
237 XmlSerializerNamespaces namespaces =
238 CreateEmptyNamespaces();
240 XmlSerializer serializer =
245 using XmlWriter writer =
250 serializer.Serialize(
257 catch (InvalidOperationException exception)
259 throw new InvalidOperationException(
260 "The lot-sizing instance could not be " +
261 "serialized to XML.",
290 bool validateBeforeSerialization =
true,
291 bool validateCurrentFingerprint =
true,
294 ArgumentNullException.ThrowIfNull(instance);
306 validateBeforeSerialization:
307 validateBeforeSerialization,
309 validateCurrentFingerprint:
310 validateCurrentFingerprint,
349 bool validateAfterDeserialization =
true,
350 bool validateCurrentFingerprint =
true)
352 if (
string.IsNullOrWhiteSpace(filePath))
354 throw new ArgumentException(
355 "A source file path is required.",
359 string fullFilePath =
363 if (!File.Exists(fullFilePath))
365 throw new FileNotFoundException(
366 "The lot-sizing instance file was not found.",
381 validateAfterDeserialization:
382 validateAfterDeserialization,
384 validateCurrentFingerprint:
385 validateCurrentFingerprint);
425 bool validateAfterDeserialization =
true,
426 bool validateCurrentFingerprint =
true)
428 ArgumentNullException.ThrowIfNull(stream);
432 throw new ArgumentException(
433 "The source stream must be readable.",
437 XmlReaderSettings readerSettings =
438 CreateReaderSettings();
440 using XmlReader reader =
445 return DeserializeFromReader(
449 validateAfterDeserialization:
450 validateAfterDeserialization,
452 validateCurrentFingerprint:
453 validateCurrentFingerprint);
483 bool validateAfterDeserialization =
true,
484 bool validateCurrentFingerprint =
true)
486 if (
string.IsNullOrWhiteSpace(xml))
488 throw new ArgumentException(
489 "An XML document is required.",
493 using var stringReader =
497 XmlReaderSettings readerSettings =
498 CreateReaderSettings();
500 using XmlReader reader =
505 return DeserializeFromReader(
509 validateAfterDeserialization:
510 validateAfterDeserialization,
512 validateCurrentFingerprint:
513 validateCurrentFingerprint);
518 bool validateAfterDeserialization,
519 bool validateCurrentFingerprint)
521 XmlSerializer serializer =
524 var document =
LotSizingDataModel.Core.Serialization.TransportXmlMigration.ReadAndMigrate(reader);
525 using var migratedReader = document.CreateReader();
526 object? deserializedObject;
531 serializer.Deserialize(
534 catch (InvalidOperationException exception)
536 throw new InvalidOperationException(
537 "The XML document could not be deserialized " +
538 "as a lot-sizing instance.",
542 if (deserializedObject is not
545 throw new InvalidOperationException(
546 "The XML document does not contain a valid " +
547 "lot-sizing instance root object.");
550 if (validateAfterDeserialization)
556 validateCurrentFingerprint:
557 validateCurrentFingerprint);
563 private static XmlSerializer CreateSerializer()
565 return new XmlSerializer(
566 typeof(LotSizingInstance));
569 private static XmlSerializerNamespaces
570 CreateEmptyNamespaces()
573 new XmlSerializerNamespaces();
582 private static XmlWriterSettings CreateWriterSettings(
585 return new XmlWriterSettings
600 NewLineHandling.None,
607 private static XmlReaderSettings CreateReaderSettings()
609 return new XmlReaderSettings
612 DtdProcessing.Prohibit,
Adds explicit closed-loop return streams to a lot-sizing instance without changing the historical Sup...
Serializes and deserializes complete lot-sizing problem instances using XML.
static Encoding XmlEncoding
Gets the encoding used when writing XML instance files.
static LotSizingInstance DeserializeFromFile(string filePath, bool validateAfterDeserialization=true, bool validateCurrentFingerprint=true)
Deserializes a lot-sizing instance from an XML file.
static LotSizingInstance Deserialize(Stream stream, bool validateAfterDeserialization=true, bool validateCurrentFingerprint=true)
Deserializes a lot-sizing instance from a readable stream.
static void SerializeToFile(LotSizingInstance instance, string filePath, bool validateBeforeSerialization=true, bool validateCurrentFingerprint=true, bool indent=true)
Serializes an instance to an XML file.
static string SerializeToString(LotSizingInstance instance, bool validateBeforeSerialization=true, bool validateCurrentFingerprint=true, bool indent=true)
Serializes an instance to an XML string.
static LotSizingInstance DeserializeFromString(string xml, bool validateAfterDeserialization=true, bool validateCurrentFingerprint=true)
Deserializes a lot-sizing instance from an XML string.
static void Serialize(LotSizingInstance instance, Stream stream, bool validateBeforeSerialization=true, bool validateCurrentFingerprint=true, bool indent=true)
Serializes an instance to a writable stream.
Validates the structural and referential consistency of a complete lot-sizing problem instance.
static void EnsureValid(LotSizingInstance instance, bool validateCurrentFingerprint=true)
Validates a lot-sizing instance and throws an exception when at least one error is found.