Created
December 25, 2025 13:54
-
-
Save mustafabutt-dev/f9128472d4f665fe6a5d88f6eb8ebe23 to your computer and use it in GitHub Desktop.
Mastering Advanced Options in C# with Groupdocs.conversion to Convert JSON to XML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.IO; | |
| using System.Text; | |
| using System.Xml; | |
| using GroupDocs.Conversion; | |
| using GroupDocs.Conversion.Options.Convert; | |
| using GroupDocs.Conversion.Options.Load; | |
| namespace JsonToXmlConversion | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Path to the source JSON file | |
| string jsonPath = @"C:\Data\input.json"; | |
| // Path for the resulting XML file | |
| string xmlOutputPath = @"C:\Data\output.xml"; | |
| // Prepare load options for large files | |
| var loadOptions = new LoadOptions | |
| { | |
| // Adjust buffer size as needed (bytes) | |
| BufferSize = 8192 | |
| }; | |
| // Configure WebConvertOptions for XML output | |
| var webOptions = new WebConvertOptions | |
| { | |
| TargetFormat = WebFormat.Xml, | |
| RootElementName = "Root", | |
| Encoding = Encoding.UTF8, | |
| XmlWriterSettings = new XmlWriterSettings | |
| { | |
| Indent = true, | |
| IndentChars = " ", | |
| OmitXmlDeclaration = false | |
| } | |
| }; | |
| try | |
| { | |
| // Open a file stream for the JSON input (read‑only, asynchronous) | |
| using (var jsonStream = new FileStream(jsonPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192, true)) | |
| // Create a file stream for the XML output (create or overwrite) | |
| using (var xmlStream = new FileStream(xmlOutputPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true)) | |
| // Initialize the Converter with the input stream and load options | |
| using (var converter = new Converter(jsonStream, loadOptions)) | |
| { | |
| // Perform the conversion synchronously | |
| var result = converter.Convert(xmlStream, webOptions); | |
| if (result.IsSuccessful) | |
| { | |
| Console.WriteLine($"Conversion succeeded. XML saved to: {xmlOutputPath}"); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"Conversion failed: {result.ErrorMessage}"); | |
| } | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| Console.WriteLine($"Exception during conversion: {ex.Message}"); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment