How to Convert XML to JSON in C# .NET Core Framework
With a free, low-code API at our disposal, we can trivialize XML file conversions to JSON format for any of our C# .NET Core applications.
To make our conversion, we’ll just need to copy from ready-to-run code examples (provided below) and acquire a free API key to authorize our requests.
With a free API key, we can make up to 800 conversions per month with zero additional commitments.
Let’s begin by installing the .NET Core SDK. We can run the below command in our package manager console to install via NuGet:
Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1
Right after that, let’s copy the below code into our file to call the function. We can provide our API key and parse our XML file bytes into their respective snippets:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDataXmlToJsonExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ConvertDataApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
try
{
// Convert XML to JSON conversion
Object result = apiInstance.ConvertDataXmlToJson(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDataApi.ConvertDataXmlToJson: " + e.Message );
}
}
}
}
Now we can quickly and easily convert XML files to JSON objects with minimal code.