How to Extract Text from XML in C#

This short article explains every step in detail to extract Text from XML in C# and guides you on how to use these stepwise instructions for developing the application to get Text from XML using C#. This post also guides you on how to configure the environment for extracting text from documents. Below you can take a look at the main steps along with the working example to read text from XML using C#.

Steps to Extract Text from XML in C#

  1. Install the GroupDocs.Parser for .NET package from the NuGet package manager in the .NET project to extract Text from XML file
  2. Add a reference to the necessary namespaces for extracting text from the XML file
  3. Create an instance of the Parser class for loading the input XML document
  4. Invoke the GetText method of the Parser class for getting the TextReader object
  5. Finally, read text from the TextReader and display it on the screen

In the preceding section, we have explained the workflow for creating the C# get Text from XML functionality. First of all, you have to set up the required package and import the necessary namespaces for extracting text from the document. In the next steps, the Parser class should be initiated for loading the input XML file, use the GetText method of it to obtain the TextReader object, and then read the text from the reader.

Code to Extract Text from XML in C#

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using GroupDocs.Parser;
using GroupDocs.Parser.Data;
namespace ExtractTextFromXmlInCSharp
{
class Program
{
public static void Main(string[] args) // Main function to extract Text from XML using C#
{
// Remove the watermark in output
string licensePath = "GroupDocs.Parser.lic";
GroupDocs.Parser.License lic = new GroupDocs.Parser.License();
lic.SetLicense(licensePath);
// Create an instance of Parser class
using (Parser parser = new Parser("sample.xml"))
{
// Extract a text into the reader
using(TextReader reader = parser.GetText())
{
// Print a text from the document
// If text extraction isn't supported, a reader is null
Console.WriteLine(reader == null ? "Text extraction isn't supported" : reader.ReadToEnd());
}
}
}
}
}

The above code snippet demonstrates how to develop the extract text from XML using C# application. The sample code shows that the functionality is created with a few lines of code that consists of simple API calls. Moreover, you can use this code on any of the common operating systems like Windows, Linux, and macOS that support .NET environment even without setting up any other third-party tool.

We have discussed the detailed instructions to get text from XML in C# and developed a sample application for it. Recently, we published an article for extracting Text from PowerPoint in C#, have a look at how to Extract Text from PowerPoint using C# guide for more information.

 English