-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegrate-xml-serialization-of-barcode-settings-into-web-api-that-accepts-configuration-json-and-returns-xml.cs
More file actions
90 lines (80 loc) · 3.25 KB
/
Copy pathintegrate-xml-serialization-of-barcode-settings-into-web-api-that-accepts-configuration-json-and-returns-xml.cs
File metadata and controls
90 lines (80 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Title: XML serialization of barcode settings via a web‑API style console demo
// Description: Demonstrates deserializing barcode configuration from JSON, generating a barcode, and exporting its settings to XML.
// Prompt: Integrate XML serialization of barcode settings into a web API that accepts configuration JSON and returns XML.
// Tags: barcode, symbology, serialization, xml, json, aspose.barcode, webapi
using System;
using System.IO;
using System.Text.Json;
using Aspose.BarCode;
using Aspose.BarCode.Generation;
using Aspose.BarCode.BarCodeRecognition;
using Aspose.Drawing;
/// <summary>
/// Simple DTO that represents the JSON payload received by the API.
/// </summary>
class Config
{
/// <summary>
/// The name of the barcode symbology (e.g., "Code128").
/// </summary>
public string Symbology { get; set; }
/// <summary>
/// The text to encode in the barcode.
/// </summary>
public string CodeText { get; set; }
}
/// <summary>
/// Console application that mimics the core logic of a web API endpoint.
/// </summary>
class Program
{
/// <summary>
/// Entry point that deserializes JSON, generates a barcode, and returns its settings as XML.
/// </summary>
static void Main()
{
// NOTE: The snippet runner is a console application. In a real web API this logic would be inside a controller action.
// Sample JSON configuration (normally this would come from an HTTP request body)
string jsonConfig = @"{ ""Symbology"": ""Code128"", ""CodeText"": ""Sample123"" }";
// Deserialize JSON to a configuration object
Config config = JsonSerializer.Deserialize<Config>(jsonConfig);
if (config == null)
{
Console.WriteLine("Invalid configuration.");
return;
}
// Resolve the symbology name to a BaseEncodeType using reflection
var field = typeof(EncodeTypes).GetField(config.Symbology);
if (field == null)
{
Console.WriteLine($"Unknown symbology: {config.Symbology}");
return;
}
BaseEncodeType encodeType = (BaseEncodeType)field.GetValue(null);
// Create the barcode generator with the resolved type and codetext
using (var generator = new BarcodeGenerator(encodeType, config.CodeText))
{
// Example of setting a barcode property (optional)
generator.Parameters.Barcode.BarColor = Color.Black;
generator.Parameters.BackColor = Color.White;
// Export the barcode settings to XML (in-memory)
using (var memoryStream = new MemoryStream())
{
bool exported = generator.ExportToXml(memoryStream);
if (!exported)
{
Console.WriteLine("Failed to export barcode settings to XML.");
return;
}
// Reset stream position and read the XML content
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
string xmlOutput = reader.ReadToEnd();
Console.WriteLine("Exported XML:");
Console.WriteLine(xmlOutput);
}
}
}
}
}