Query an XML file using XQuery, Get Results in C#

Cloudmersive
2 min readFeb 25, 2022

Easily dig deeper into XML files with the Cloudmersive Convert API. With the convert/xml/query/xquery iteration, you can return results for a single XML document with an XQuery expression. The parameters require a valid XML XQuery 3.1 (or earlier) expression. Let’s walk through how to take advantage in this API using C# code from the Cloudmersive API Console.

The first step will differ on .NET vs. .NET Core. If you’re using the former, first use the below snippet to install the .NET SDK:

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;

For the latter, use this .NET Core snippet:

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;

After that, copy & paste the below code and provide your API key where indicated. If you don’t have a Cloudmersive API key yet, head to the Cloudmersive website and create a free account (no commitments) to receive one.

namespace Example
{
public class ConvertDataXmlQueryWithXQueryExample
{
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 xQuery = xQuery_example; // string | Valid XML XQuery 3.1 or earlier query expression; multi-line expressions are supported
try
{
// Query an XML file using XQuery query, get results
XmlQueryWithXQueryResult result = apiInstance.ConvertDataXmlQueryWithXQuery(inputFile, xQuery);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDataApi.ConvertDataXmlQueryWithXQuery: " + e.Message );
}
}
}
}

--

--

Cloudmersive

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