Perform CSV to XML Conversion in C#

Cloudmersive
2 min readFeb 24, 2022

Dozens of unique data conversions are made easy with the Cloudmersive Convert API. Take advantage of ready-to-run code (below) from the Cloudmersive API Console to easily convert a CSV file to an XML file in C#.

First, let’s install the .NET SDK using the below command:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;

If you’re using .NET Core, use this command instead:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

Then, copy the remaining code & supply your Cloudmersive API Key where indicated. To get a key, you can make a free (zero commitment) account on the Cloudmersive website, which will enable 800 API calls per month.

namespace Example
{
public class ConvertDataCsvToXmlExample
{
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.
var columnNamesFromFirstRow = true; // bool? | Optional; If true, the first row will be used as the labels for the columns; if false, columns will be named Column0, Column1, etc. Default is true. Set to false if you are not using column headings, or have an irregular column structure. (optional)
try
{
// Convert CSV to XML conversion
byte[] result = apiInstance.ConvertDataCsvToXml(inputFile, columnNamesFromFirstRow);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDataApi.ConvertDataCsvToXml: " + e.Message );
}
}
}
}

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.