# Using XML Template

## Content

**TextParser** library provides [TemplateBasedExtractor](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.TemplateBasedExtractor.html) class to set up the **Template-Based extractor** that allows you to parse a plain text document following any user defined structure format.
The structure format is a template which is specified following a declarative approach that is XML. The plain text input to parse can contain many instances of the defined template. All the text that matches the specification of the template can be extracted from the input text.
This section helps you get started on how to define your custom Template-Based extractor templates.

### Defining a template

The template to be used for text extraction is defined formally using XML elements/tags and its properties. The root of any XML template definition must be a **template** XML element. The extraction can be performed either by defining properties for the “template” element or by nesting the template element to define complex user-defined structures. Following are the different template structures for the text extraction process:

* [Simple Template](/componentone/docs/services/online-textparser/working/xmltemplate/simpletemplate)
* [Nested Template](/componentone/docs/services/online-textparser/working/xmltemplate/nestedtemplate)
* [Advanced Template](/componentone/docs/services/online-textparser/working/xmltemplate/advancedtemplate)

### Applying a template

To extract the text using [TemplateBasedExtractor](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.TemplateBasedExtractor.html) class, you need to implement the steps mentioned in the code snippet below:

1. Open the plain text template file which contains the user defined XML template. **csharp**

    ```csharp
    Stream templateStream = File.Open(@"Template.xml", FileMode.Open);
    ```
2. Create an instance of the [TemplateBasedExtractor](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.TemplateBasedExtractor.html) class and pass the stream containing the user defined XML template as a parameter to it. **csharp**

    ```csharp
    TemplateBasedExtractor extractionResult = new TemplateBasedExtractor(templateStream);
    ```
3. Open the plain text input source file from which you want to extract the text. **csharp**

    ```csharp
    Stream inputStream = File.Open(@"Source.txt", FileMode.Open);
    ```
4. Extract the desired text from the input source using the [Extract](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.TemplateBasedExtractor.Extract.html) method of the [TemplateBasedExtractor](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.TemplateBasedExtractor.html) class. This method returns an instance of [IExtractionResult](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.IExtractionResult.html) interface containing the extraction results. **csharp**

    ```csharp
    IExtractionResult res = extractionResult.Extract(inputStream);
    ```
5. To convert the extraction result to JSON string, use the [ToJsonString](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.IExtractionResult.ToJsonString.html) method of the [IExtractionResult](/componentone/api/services/online-textparser/dotnet-standard-api/C1.TextParser/C1.TextParser.IExtractionResult.html) interface. **csharp**

    ```csharp
    Console.WriteLine(res.ToJsonString());
    ```

After defining and applying the XML template through code, the parsed result is obtained in the JSON string format which can be further used as the extracted text from the input source.