/// <summary> /// Serialization XML of an objet into a compressed file. /// </summary> private static void TestSerialisationXMLToCompressedFile() { // If the output file alreary exists, delete it FileInfo fileInfo = new FileInfo(myOutputXMLFileFullname); fileInfo.Delete(); fileInfo = new FileInfo(myOutputXMLZIPFileFullname); fileInfo.Delete(); // Object instanciation and initialization. ReplacedField O = new ReplacedField(); // XML serializer creation. XmlSerializer serializer = new XmlSerializer(typeof(ReplacedField)); // Flux creation. Stream stream = new FileStream(myOutputXMLFileFullname, FileMode.Create); // Object serialisation into the flux. serializer.Serialize(stream, O); stream.Close(); stream.Dispose(); // File compression. fileInfo = new FileInfo(myOutputXMLFileFullname); String startPath = fileInfo.DirectoryName; String zipPath = myOutputXMLZIPFileFullname; ZipFile.CreateFromDirectory(startPath, zipPath); }
/// <summary> /// Deserialization XML of an objet from a compressed file /// </summary> private static void TestDeserialisationCompressedXMLFileToObject() { // If the output uncompressed file alreary exists, delete it FileInfo fileInfo = new FileInfo(myOutputXMLFileFullname); fileInfo.Delete(); // File decompression String zipPath = myOutputXMLZIPFileFullname; fileInfo = new FileInfo(myOutputXMLFileFullname); String extractPath = fileInfo.DirectoryName; ZipFile.ExtractToDirectory(zipPath, extractPath); // XML serializer creation XmlSerializer xmlSerializer = new XmlSerializer(typeof(ReplacedField)); // Flux creation Stream stream = new FileStream(myOutputXMLFileFullname, FileMode.Open); // Object deserialization from the flux ReplacedField O = (ReplacedField)xmlSerializer.Deserialize(stream); stream.Close(); stream.Dispose(); // To check, display of object Console.WriteLine($"Pattern : {O.Pattern} \n" + $"Field : {O.Field} \n" + $"HasChanged : {O.HasChanged}"); }
/// <summary> /// Add objet serialized in XML to an XML file /// </summary> private static void testXMLAdd2() { // Configuration and reset string outputFile = @"C:\Temp\ExempleDuLivre\Chapitre24.Serialisation\Tests\PurchaseOrderAdded2.xml"; FileInfo fileInfo = new FileInfo(outputFile); fileInfo.Delete(); // ---------------------Top XML example XElement xElement = XElement.Load(@"C:\Temp\ExempleDuLivre\Chapitre24.Serialisation\Tests\PurchaseOrder.xml"); // ---------------------Bottom XML built from an object // XML to add from an object // Object instanciation and initialization. ReplacedField o = new ReplacedField(); //// XML serializer creation. //XmlSerializer serializer = new XmlSerializer(typeof(ReplacedField)); //// Flux creation //Stream stream = new FileStream(myOutputXMLFileFullname, FileMode.Create); //// Object serialisation into the flux. //serializer.Serialize(stream, O); //stream.Close(); //stream.Dispose(); // Get the serialized XElement of an object XElement xElement1 = Serializer <ReplacedField> .GetXElementFromObject(o); // Adding a first object xElement.Add(new XElement("NewChid", "new content")); xElement.Add(xElement1); // Adding a second object Observation ob = new Observation { Date = DateTime.Now, Enregistrement = "MyRecording" }; XElement xElement2 = Serializer <Observation> .GetXElementFromObject(ob); xElement.Add(xElement2); xElement.Save(outputFile); Console.WriteLine($"The output file is {outputFile}"); }