---
title: "Read and write a CoNLL-U file in C#"
url: https://recursiveai.org/onenlp/cookbook/round-trip-conllu-annotation-files/
description: "Round-trip a CoNLL-U annotation file in C# with OneNLP. Preserve sentence comments, multiword-token rows, and empty nodes by writing the full sentence object."
version: "1.0.0"
---

# Read and write a CoNLL-U file in C#

**OneNLP** is a free, open-source natural language processing library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneNlp) · [GitHub](https://github.com/recursive-ai-oss/onenlp-csharp) · [Docs](https://recursiveai.org/onenlp/docs/) · [Download example (.zip)](https://recursiveai.org/onenlp/cookbook/round-trip-conllu-annotation-files/round-trip-conllu-annotation-files.zip)

CoNLL-U stores grammar annotations in tab-separated rows. Write the whole sentence object to keep comments and special rows.

```shell
dotnet add package RecursiveAI.OneNlp
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneNlp.Data;

// sample.conllu is in the zip.

using (var output =
    File.CreateText("out.conllu"))
{
    foreach (var sentence in
        ConllUReader.Read("sample.conllu"))
        ConllUWriter.Write(output, sentence);
}
Console.WriteLine("Wrote out.conllu");
```

[The example](https://recursiveai.org/onenlp/cookbook/round-trip-conllu-annotation-files/round-trip-conllu-annotation-files.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Prints:

```text
Wrote out.conllu
```

The zip includes this `sample.conllu`. To recreate it, keep the tabs between fields and leave a blank line after the last row.

```text
# sent_id = demo-1
# text = I can't sing, but Bea can.
1	I	I	PRON	_	_	4	nsubj	4:nsubj	_
2-3	can't	_	_	_	_	_	_	_	_
2	ca	can	AUX	_	_	4	aux	4:aux	_
3	n't	not	PART	_	_	4	advmod	4:advmod	_
4	sing	sing	VERB	_	_	0	root	0:root	SpaceAfter=No
5	,	,	PUNCT	_	_	8	punct	8.1:punct	_
6	but	but	CCONJ	_	_	8	cc	8.1:cc	_
7	Bea	Bea	PROPN	_	_	8	nsubj	8.1:nsubj	_
8	can	can	AUX	_	_	4	conj	8.1:aux	SpaceAfter=No
8.1	sing	sing	VERB	_	_	_	_	4:conj	_
9	.	.	PUNCT	_	_	4	punct	4:punct	_

```

## Full program

```csharp
using RecursiveAI.OneNlp.Data;

// sample.conllu is in the zip.

using (var output =
    File.CreateText("out.conllu"))
{
    foreach (var sentence in
        ConllUReader.Read("sample.conllu"))
        ConllUWriter.Write(output, sentence);
}
Console.WriteLine("Wrote out.conllu");
var original = File.ReadAllBytes("sample.conllu");
var written = File.ReadAllBytes("out.conllu");
if (!original.AsSpan().SequenceEqual(written))
    throw new InvalidDataException("Round trip changed the file.");

var restored = ConllUReader.Read("out.conllu").Single();
Console.WriteLine($"Comments: {restored.Comments.Count}");
Console.WriteLine($"Multiword token: {restored.MultiwordTokens.Single().Form}");
Console.WriteLine($"Empty node: {restored.EmptyNodes.Single().Line.Split('\t')[0]}");
Console.WriteLine("Byte-for-byte match");
```

`dotnet run` prints:

```text
Wrote out.conllu
Comments: 2
Multiword token: can't
Empty node: 8.1
Byte-for-byte match
```

The full example checks the bytes after the writer closes, then reads the output to report the preserved comments, token range, and empty node. If a row has the wrong number of columns, the reader throws `DataFormatException` with its line number during enumeration.

## Variations

**Read the sentence ID.** Use `GetComment` to retrieve the value without parsing the `# sent_id =` line yourself.

```csharp
foreach (var sentence in
    ConllUReader.Read("sample.conllu"))
    Console.WriteLine(sentence.GetComment("sent_id"));
```

```text
demo-1
```

## Related recipes

- [Read CoNLL-U training data](https://recursiveai.org/onenlp/cookbook/read-conllu-training-data/)
- [Create a document from existing tokens](https://recursiveai.org/onenlp/cookbook/create-doc-from-existing-tokens/)

## The short answer

Read with `ConllUReader.Read(path)` and pass the returned `ConllUSentence` to `ConllUWriter.Write(output, sentence)`. Don’t pass only `sentence.Doc`: that overload writes words and their basic annotations, not the sentence comments, range rows, empty nodes, or enhanced dependency column you read in. Use a separate output path so opening the writer doesn’t truncate the input.

## What is the CoNLL-U format?

The [Universal Dependencies format](https://universaldependencies.org/format.html) uses UTF-8, LF line endings, and a blank line after each sentence. Rows have ten columns separated by tabs, not spaces. The columns hold the ID, written form, lemma, two part-of-speech tags, morphological features, basic dependency head and relation, enhanced dependencies, and miscellaneous annotations. An underscore marks an unspecified field. Lines starting with `#` before the rows store metadata, such as the sentence ID and original text.

## What are multiword tokens and empty nodes in CoNLL-U?

A range such as `2-3` describes one surface token, `can’t`, that the syntax treats as two words. The integer rows `2` and `3` carry the annotations for `ca` and `n’t`. An empty node such as `8.1` goes the other way: it exists in the enhanced dependency graph but not in the written sentence. Here it supplies the omitted `sing` after `Bea can`. It has no basic `HEAD` or `DEPREL`; its enhanced edge is in `DEPS`. Neither kind of row is an extra integer-indexed word in OneNLP’s `Doc`. Keep the `ConllUSentence` so the writer has the range and the empty-node line as well as the word annotations.

## When does a CoNLL-U round trip reproduce the original file?

The example’s unchanged file matches byte for byte. Don’t use that as a promise for arbitrary input: the reader can skip a UTF-8 byte-order mark and the writer emits LF and a blank line after each sentence. It also reconstructs multiword rows from the stored range, form, and `MISC`, writing underscores in the other columns. In particular, the format’s optional `Typo=Yes` in `FEATS` on a multiword row doesn’t survive that reconstruction in this version. For a large file, keep the `foreach` read/write loop and omit the demo’s byte-array comparison; the reader yields sentences as it reads them.
