5using System.Xml.Serialization;
17 private static readonly Encoding XmlEncoding =
19 encoderShouldEmitUTF8Identifier:
false);
21 private static readonly XmlSerializer Serializer =
44 _validator = validator ??
45 throw new ArgumentNullException(nameof(validator));
68 bool validateBeforeSave =
true)
70 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
71 ArgumentNullException.ThrowIfNull(supplyChain);
74 Path.GetFullPath(filePath);
76 string? directoryPath =
77 Path.GetDirectoryName(fullPath);
79 if (
string.IsNullOrWhiteSpace(directoryPath))
81 throw new InvalidOperationException(
82 "The destination directory cannot be determined.");
85 Directory.CreateDirectory(directoryPath);
88 string temporaryFilePath =
92 Path.GetFileName(fullPath) +
94 Guid.NewGuid().ToString(
"N") +
99 using (var stream =
new FileStream(
123 if (File.Exists(temporaryFilePath))
125 File.Delete(temporaryFilePath);
151 bool validateAfterLoad =
true,
152 bool synchronizePlanningHorizon =
false)
154 ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
157 Path.GetFullPath(filePath);
159 if (!File.Exists(fullPath))
161 throw new FileNotFoundException(
162 "The supply-chain XML file does not exist.",
166 using var stream =
new FileStream(
175 synchronizePlanningHorizon);
195 bool validateBeforeSerialization =
true)
197 ArgumentNullException.ThrowIfNull(output);
198 ArgumentNullException.ThrowIfNull(supplyChain);
200 if (!output.CanWrite)
202 throw new ArgumentException(
203 "The output stream must be writable.",
207 if (validateBeforeSerialization)
209 _validator.ThrowIfInvalid(supplyChain);
214 new XmlSerializerNamespaces();
224 var settings =
new XmlWriterSettings
226 Encoding = XmlEncoding,
229 NewLineChars = Environment.NewLine,
230 NewLineHandling = NewLineHandling.Replace,
231 OmitXmlDeclaration =
false,
235 using XmlWriter writer =
240 Serializer.Serialize(
266 bool validateAfterDeserialization =
true,
267 bool synchronizePlanningHorizon =
false)
269 ArgumentNullException.ThrowIfNull(input);
273 throw new ArgumentException(
274 "The input stream must be readable.",
278 var settings =
new XmlReaderSettings
284 DtdProcessing = DtdProcessing.Prohibit,
286 IgnoreComments =
true,
287 IgnoreProcessingInstructions =
true,
291 using XmlReader reader =
297 using var migratedReader = document.CreateReader();
299 Serializer.Deserialize(migratedReader);
303 throw new InvalidOperationException(
304 "The XML document does not contain a valid " +
305 "supply-chain root object.");
313 if (synchronizePlanningHorizon)
315 supplyChain.SynchronizePlanningHorizon();
318 if (validateAfterDeserialization)
320 _validator.ThrowIfInvalid(supplyChain);
340 bool validateBeforeSerialization =
true)
342 ArgumentNullException.ThrowIfNull(supplyChain);
344 using var stream =
new MemoryStream();
349 validateBeforeSerialization);
351 return XmlEncoding.GetString(
373 bool validateAfterDeserialization =
true,
374 bool synchronizePlanningHorizon =
false)
376 ArgumentException.ThrowIfNullOrWhiteSpace(xml);
379 XmlEncoding.GetBytes(xml);
388 validateAfterDeserialization,
389 synchronizePlanningHorizon);
void Save(string filePath, SupplyChain supplyChain, bool validateBeforeSave=true)
Saves a supply chain to an XML file.
SupplyChain DeserializeFromString(string xml, bool validateAfterDeserialization=true, bool synchronizePlanningHorizon=false)
Deserializes a supply chain from an XML string.
SupplyChain Load(string filePath, bool validateAfterLoad=true, bool synchronizePlanningHorizon=false)
Loads a supply chain from an XML file.
SupplyChainXmlSerializer()
Initializes the serializer with a default supply-chain validator.
string SerializeToString(SupplyChain supplyChain, bool validateBeforeSerialization=true)
Serializes a supply chain to an XML string.
SupplyChain Deserialize(Stream input, bool validateAfterDeserialization=true, bool synchronizePlanningHorizon=false)
Deserializes a supply chain from an input stream.
SupplyChainXmlSerializer(SupplyChainValidator validator)
Initializes the serializer with the specified validator.
void Serialize(Stream output, SupplyChain supplyChain, bool validateBeforeSerialization=true)
Serializes a supply chain to an output stream.
Lossless, in-memory migration of legacy nested transport lanes. Input files are never written.
static XDocument ReadAndMigrate(XmlReader reader)
Loads XML through the caller's secure reader and returns a migrated copy.
Represents the complete supply-chain data model.
Validates the structural, referential and decision-model consistency of a complete supply chain.