5using System.Xml.Serialization;
6using LotSizingDataModel.Core;
24 private readonly XmlSerializer _serializer;
49 bool indentOutput =
true)
51 ArgumentNullException.ThrowIfNull(validator);
53 _validator = validator;
90 bool validateBeforeSave =
false)
92 ArgumentException.ThrowIfNullOrWhiteSpace(
95 ArgumentNullException.ThrowIfNull(solution);
98 Path.GetFullPath(filePath);
100 string? directoryPath =
101 Path.GetDirectoryName(fullPath);
103 if (!
string.IsNullOrWhiteSpace(
106 Directory.CreateDirectory(
145 SupplyChain supplyChain)
147 ArgumentNullException.ThrowIfNull(solution);
148 ArgumentNullException.ThrowIfNull(supplyChain);
150 _validator.ThrowIfInvalid(
157 validateBeforeSave:
false);
180 bool validateBeforeSave =
false,
181 bool leaveOpen =
true)
183 ArgumentNullException.ThrowIfNull(stream);
184 ArgumentNullException.ThrowIfNull(solution);
186 if (!stream.CanWrite)
188 throw new ArgumentException(
189 "The destination stream is not writable.",
193 if (validateBeforeSave)
195 _validator.ThrowIfInvalid(solution);
198 XmlWriterSettings settings =
199 CreateWriterSettings(leaveOpen);
201 using XmlWriter writer =
206 _serializer.Serialize(
235 bool validateAfterLoad =
false)
237 ArgumentException.ThrowIfNullOrWhiteSpace(
241 Path.GetFullPath(filePath);
276 SupplyChain supplyChain)
278 ArgumentNullException.ThrowIfNull(supplyChain);
283 validateAfterLoad:
false);
285 _validator.ThrowIfInvalid(
315 bool validateAfterLoad =
false,
316 bool leaveOpen =
true)
318 ArgumentNullException.ThrowIfNull(stream);
322 throw new ArgumentException(
323 "The source stream is not readable.",
327 XmlReaderSettings settings =
328 CreateReaderSettings(leaveOpen);
332 using XmlReader reader =
340 if (validateAfterLoad)
342 _validator.ThrowIfInvalid(solution);
347 catch (XmlException exception)
349 throw new InvalidDataException(
350 "The XML document is malformed.",
353 catch (InvalidOperationException exception)
354 when (exception.InnerException is XmlException)
356 throw new InvalidDataException(
357 "The XML document does not contain a valid " +
358 "lot-sizing solution.",
378 bool validateBeforeSerialization =
false)
380 ArgumentNullException.ThrowIfNull(solution);
388 validateBeforeSerialization,
391 return Encoding.UTF8.GetString(
410 bool validateAfterDeserialization =
false)
412 ArgumentException.ThrowIfNullOrWhiteSpace(xml);
415 Encoding.UTF8.GetBytes(xml);
424 validateAfterDeserialization,
431 object? deserializedObject =
432 _serializer.Deserialize(reader);
434 if (deserializedObject is not
437 throw new InvalidDataException(
438 "The XML document does not contain a " +
439 "lot-sizing solution.");
447 private XmlWriterSettings CreateWriterSettings(
450 return new XmlWriterSettings
454 encoderShouldEmitUTF8Identifier:
false),
457 OmitXmlDeclaration =
false,
458 CloseOutput = !leaveOpen,
460 NewLineHandling.Entitize
464 private static XmlReaderSettings CreateReaderSettings(
467 return new XmlReaderSettings
470 DtdProcessing.Prohibit,
473 CloseInput = !leaveOpen,
474 IgnoreComments =
true,
475 IgnoreProcessingInstructions =
true
Represents one candidate solution for a lot-sizing supply-chain instance.
void ReconnectNestedNotifications()
string SerializeToString(LotSizingSolution solution, bool validateBeforeSerialization=false)
Serializes a solution to an XML string.
LotSizingSolution Load(string filePath, bool validateAfterLoad=false)
Loads a solution from an XML file.
bool IndentOutput
Gets a value indicating whether generated XML is indented.
LotSizingSolution Load(string filePath, SupplyChain supplyChain)
Loads a solution from an XML file and validates it against a supply-chain instance.
void Save(string filePath, LotSizingSolution solution, bool validateBeforeSave=false)
Saves a solution to an XML file.
void Save(string filePath, LotSizingSolution solution, SupplyChain supplyChain)
Validates a solution against a supply-chain instance and saves it to an XML file.
LotSizingSolution DeserializeFromString(string xml, bool validateAfterDeserialization=false)
Deserializes a solution from an XML string.
void Save(Stream stream, LotSizingSolution solution, bool validateBeforeSave=false, bool leaveOpen=true)
Saves a solution to a writable stream.
LotSizingSolutionXmlSerializer(LotSizingSolutionValidator validator, bool indentOutput=true)
Initializes a solution XML serializer.
LotSizingSolution Load(Stream stream, bool validateAfterLoad=false, bool leaveOpen=true)
Loads a solution from a readable stream.
LotSizingSolutionXmlSerializer()
Initializes a solution XML serializer using an indented XML representation.
Validates the structure and instance compatibility of a lot-sizing solution.