Export and Import Barcode Generation State via XML in C#


In Aspose.BarCode for .NET, it is possible to perform the serialization of the barcode generation state and import it from XML using the specific functionality of class BarcodeGenerator. The serialization of a BarcodeGenerator instance can be executed in two ways: saving it to an XML-file using the ExportToXml(String) or to a stream through the ExportToXml(Stream) method.

Similarly, loading the barcode generation state from XML can be performed from a file using the ImportFromXml(String) method or a stream through the ImportFromXml(Stream) method.

Export Barcode Generation State to XML

As previously mentioned, there are two ways to save the current state of class BarcodeGenerator: to an XML file through the ExportToXml(String) function or to a stream using the ExportToXml(Stream) function. The code snippet below illustrates how to implement the serialization of a BarcodeGenerator instance to an XML file.

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.MacroPdf417, "Åspóse.Barcóde©");
gen.Parameters.Barcode.XDimension.Pixels = 2;
gen.Parameters.Barcode.Pdf417.Columns = 4;
//set PDF417 metadata
gen.Parameters.Barcode.Pdf417.Pdf417MacroFileID = 12345678;
gen.Parameters.Barcode.Pdf417.Pdf417MacroSegmentID = 12;
gen.Parameters.Barcode.Pdf417.Pdf417MacroSegmentsCount = 20;
gen.Parameters.Barcode.Pdf417.Pdf417MacroFileName = "file01";
gen.Parameters.Barcode.Pdf417.Pdf417MacroChecksum = 1234;
gen.Parameters.Barcode.Pdf417.Pdf417MacroFileSize = 400000;
gen.Parameters.Barcode.Pdf417.Pdf417MacroTimeStamp = new DateTime(2019, 11, 1);
gen.Parameters.Barcode.Pdf417.Pdf417MacroAddressee = "street";
gen.Parameters.Barcode.Pdf417.Pdf417MacroSender = "aspose";
//serialize BarcodeGenerator to file
//generate original
gen.Save($"{path}BarcodeGeneratorOriginal.png", BarCodeImageFormat.Png);

Import Barcode Generation State from XML

The current state of class BarcodeGenerator can be imported from an XML file using the ImportFromXml(String) function or from a stream through the ImportFromXml(Stream) function. The code sample provided below explains how to import the barcode generation state from an XML file.

//load BarcodeGenerator from file
BarcodeGenerator gen = BarcodeGenerator.ImportFromXml($"{path}generatorMacroPdf417.xml");
//generate loaded
gen.Save($"{path}BarcodeGeneratorLoaded.png", BarCodeImageFormat.Png);

Save and Load Barcode Generation State from Stream

The code snippet given below demonstrates how to save and load the barcode generation state from streams using two corresponding methods: ExportToXml(Stream) and ImportFromXml(Stream).

BarcodeGenerator gen = new BarcodeGenerator(EncodeTypes.QR, "Åspóse.Barcóde©");
gen.Parameters.Barcode.XDimension.Pixels = 4;

//save to stream
MemoryStream ms = new MemoryStream();
ms.Position = 0;

//load from stream
gen = BarcodeGenerator.ImportFromXml(ms);
//generate loaded
gen.Save($"{path}BarcodeGeneratorFromStream.png", BarCodeImageFormat.Png);