How to Convert HTML to Text in C# .NET Framework

Cloudmersive
2 min readApr 24, 2024

Rather than write a bunch of code to extract text from HTML elements, we can call a simple, low-code API instead.

Using ready-to-run C# .NET Core code examples provided below, we can structure our API call and easily convert HTML strings to plain text strings. This will simply extract text contents from HTML elements and return a block of plain text in the API response.

We can begin by installing the client SDK. Let’s run the below command in our Package Manager console to install via NuGet:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, we can quickly turn our attention to authorizing our API calls. We’ll just need a free Cloudmersive API key (this will allow a limit of 800 API calls per month with no additional commitments).

Now we can copy the below code examples to call the function:

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 ConvertWebHtmlToTxt_0Example
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertWebApi();
var input = new HtmlToTextRequest(); // HtmlToTextRequest | HTML to Text request parameters

try
{
// Convert HTML string to text (txt)
HtmlToTextResponse result = apiInstance.ConvertWebHtmlToTxt_0(input);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertWebApi.ConvertWebHtmlToTxt_0: " + e.Message );
}
}
}
}

All done — no more code required. Now we can easily convert our HTML strings to text with zero hassle.

--

--

Cloudmersive

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