How to Transform XML Documents via XSLT in C# .NET Framework
Thanks to the eXtensible Stylesheet Language Transformations (XSLT) language, we can efficiently transform and style XML documents to our liking.
With the help of a free API, we can quickly and easily apply XSLT transformations to our XML documents without relying on web browser technologies or programming libraries/modules that use up valuable local resources in the process. We can easily structure our API call by taking advantage of complementary code examples provided further down the page.
To structure our API call, we can begin by installing the SDK. To install our SDK with NuGet, let’s go ahead and run the following command in our Package Manager console:
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2
Before we implement code to call the function, let’s now grab a free API key to authorize our calls. A free API key will allow a limit of 800 API calls per month with no commitments (once we reach that limit, our total will reset the following month).
With our API key ready, let’s now copy the remaining code into our file to call the function, and then let’s supply our API key in the authorization snippet. Below that, we can provide our XML file path and XSLT file path in their respective request variables:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDataXmlTransformWithXsltToXmlExample
{
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 XML file to perform the operation on.
var transformFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input XSLT file to use to transform the input XML file.
try
{
// Transform XML document file with XSLT into a new XML document
byte[] result = apiInstance.ConvertDataXmlTransformWithXsltToXml(inputFile, transformFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDataApi.ConvertDataXmlTransformWithXsltToXml: " + e.Message );
}
}
}
}
Just like that, we now have an easy method for applying XSLT transformations to XML documents which we can slot into any of our own web applications.