Convert HTML to Text in C# .NET Core
Learn two different ways to handle HTML to plain text conversions in C# .NET Core.
Getting plain, unformatted text from any HTML string simply means stripping away the HTML elements used to format that text on a web page. It’s common to make HTML to text conversions in a variety of text-dependent workflows, like NLP analysis or web page indexing analysis, for example. There’s also a noteworthy security benefit for web applications; removing HTML is a form of text normalization, and that helps protect text inputs against Cross-Site Scripting (XSS) attacks.
Tutorial: Convert HTML to Text in C# .NET Core
In this tutorial, we’ll learn how to call two separate APIs that both convert HTML content to text in slightly different ways. They’ll simplify the HTML to Text conversion process for us significantly, and they’ll serve more casual & security-oriented circumstances respectively. One API is optimized to make direct text conversions from HTML strings, while the other API is designed to handle text input requests containing HTML and other plain text contents (i.e., potential XSS text inputs from a product review or comment section on a blog).
Step 1: Install the Cloudmersive Convert API Client
To begin our tutorial, we’ll first install the package by running the following command in our Package Manager console:
Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1
Step 2: Add the Using Statements
We’ll next copy the following using statements and add those to the top of our .NET Core file:
using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;
Step 3: Configuration & Instancing
In our third and final step, we’ll copy code examples to handle configuration, instancing, and function calls for both HTML to text conversion APIs.
Configuration for both API calls means authorizing our API calls with a Cloudmersive API key. To get a free API key, we just need to visit the Cloudmersive website and sign up for a free account. That will generate a free API key in our account management portal, and we can paste our API key string replacing the “YOUR_API_KEY” placeholder text in each API call.
Let’s use the following code to call the HTML string to text conversion API:
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 );
}
}
}
}
And let’s next use the following code to call our second API variation that removes HTML formatting from a larger text input string:
namespace Example
{
public class EditTextRemoveHtmlExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new EditTextApi();
var request = new RemoveHtmlFromTextRequest(); // RemoveHtmlFromTextRequest | Input request
try
{
// Remove HTML from text string
RemoveHtmlFromTextResponse result = apiInstance.EditTextRemoveHtml(request);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EditTextApi.EditTextRemoveHtml: " + e.Message );
}
}
}
}
That’s all there is to it - both API responses will return plain, HTML-free text content.
Enjoying these tutorials? Subscribe to our blog for API tutorials each week shared directly to your inbox.