How to remove HTML from a text string in C# .NET Framework
Even with security measures in place, scripting attacks can be a real danger. One very simple, yet powerful method to guard against this is to filter out HTML code from user submitted data. The good news — this task has never been easier.
Let’s start the ball rolling with a quick installation of Cloudmersive’s API Client via NuGet. This just means pasting the following command right into your console of the Package Manager.
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 2.2.0
Now we are ready to call our function, which is called EditTextRemoveHtml:
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 EditTextRemoveHtmlExample{public void main(){// Configure API key authorization: ApikeyConfiguration.Default.AddApiKey("Apikey", "YOUR_API_KEY");// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");var apiInstance = new EditTextApi();var request = new RemoveHtmlFromTextRequest(); // RemoveHtmlFromTextRequest | Input requesttry{// Remove HTML from text stringRemoveHtmlFromTextResponse result = apiInstance.EditTextRemoveHtml(request);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EditTextApi.EditTextRemoveHtml: " + e.Message );}}}}
The API will solve the problem for us, removing any HTML present, leaving us with pure text, cleaned of formatting.