---
title: "Raitung: XRechnung and ZUGFeRD in pure C#"
url: "https://www.dariuszparys.com/raitung-e-invoices-in-pure-csharp"
author: "Dariusz Parys"
published: 2026-12-04T10:10:03.000Z
updated: 2026-12-04T10:10:03.000Z
description: "Raitung is an open-source C# library that creates, reads and validates XRechnung and ZUGFeRD / Factur-X e-invoices without Java or native code at run time."
tags:
  - ".NET"
  - "E-Invoicing"
---

# Raitung: XRechnung and ZUGFeRD in pure C#

I just published Raitung 1.0.0-alpha.1: an open-source C# library that creates, reads and validates German and European e-invoices, without Java or native code at run time.

Since 1 January 2025, every German business must be able to receive e-invoices. From 1 January 2027, businesses with more than 800,000 EUR turnover in the previous year must also issue them for domestic B2B sales. Everyone else follows on 1 January 2028 (§27 Abs. 38 UStG). An e-invoice here means structured data following EN 16931: XRechnung XML, or a ZUGFeRD / Factur-X PDF with the XML embedded.

In a project at work, we handle this with a Python service in a container, because the .NET libraries we found could not run the official validation rules without Java. Raitung is my attempt to build a .NET library that can.

## Why a new library

The .NET options I found either covered only part of the job or needed another runtime. Validation is the hard part: the official rules are Schematron with XPath 2.0, and the usual way to run them is Saxon, in Java or as SaxonCS, a binary package with a native dependency.

I set a few constraints from the start:

- Pure C# on .NET 10, Apache-2.0 licensed. The core package has no dependencies at all.
- Trimming- and Native AOT-compatible: hand-written `XmlWriter` and `XmlReader` code, no `XmlSerializer`, no reflection.
- Deterministic output: the same input gives the same bytes, so tests can compare files byte for byte.
- Format errors (XSD, syntax) are reported separately from business-rule errors, because only the first kind means "this is not an e-invoice".

The result is five packages:

| Package              | What it does                                                                                                                  |
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `Raitung`            | EN 16931 model, totals with EN 16931 rounding, CII writer and reader, XSD validation, XRechnung helpers including Leitweg-IDs |
| `Raitung.Ubl`        | UBL 2.1 invoices and credit notes                                                                                             |
| `Raitung.Pdf`        | Turns an existing PDF into a PDF/A-3b hybrid invoice, and extracts the XML again                                              |
| `Raitung.Schematron` | Schematron engine with an XPath 2.0 subset                                                                                    |
| `Raitung.Validation` | Validation with the FeRD rules and the KoSIT scenarios for XRechnung                                                          |

## The hard part: matching Saxon

I wrote my own Schematron engine with an XPath 2.0 subset. I wanted the same verdict as the KoSIT validator and Mustang. A strict reading of the specification was less important. Both run Saxon, so where Saxon differs from the specification, Raitung follows Saxon and reports a notice.

Two examples of what that means:

- [Saxon treats an untyped `-0.00` as not equal to `0`](/notes/saxon-untyped-negative-zero-not-equal-to-zero). That changes the outcome of the rules BR-Z-09 and BR-CO-14, so the engine reproduces it.
- `System.Decimal` has 28 significant digits, which is not enough for Saxon's division rule. The engine has its own arbitrary-precision decimal that divides exactly as Saxon does.

To prove the match, the tests use the reference tools as oracles. A pipeline runs the KoSIT validator (Saxon inside) and Mustang in Docker over several corpora: the XRechnung test suite, the CEN examples, randomly generated invoices, and Raitung's own output. Both tools are needed, because [Mustang's Factur-X check does not contain all of the CEN rules](/notes/mustang-factur-x-validation-misses-cen-rules). Their findings are checked in as expectations, with a digest of the inputs. The tests compare rule IDs, flags and locations, and need neither Docker nor Java.

If the engine doesn't implement a construct, loading the rules fails with an error. It never ignores that construct and silently passes.

## Hybrid PDFs without touching the original

A ZUGFeRD invoice is a PDF/A-3 file with the invoice XML attached. Raitung doesn't render the PDF. It takes the one you already have and appends an [incremental update](/notes/pdfpig-incremental-update-af-and-random-id). The original bytes stay unchanged, and the update adds the XMP metadata, an sRGB output intent if needed, and the XML as an associated file.

Not every PDF can become PDF/A-3b that way. A pre-flight check rejects encrypted files, fonts that aren't embedded, JavaScript and similar cases with typed errors, instead of producing a file that fails later.

A conformance gate checks the result with veraPDF and Mustang in Docker. It also runs negative probes, such as an invoice with a wrong total, and requires each of them to fail for the stated reason. If a check never fails, it doesn't prove anything.

## The name, and going public

The working title was Rechnungswerk, but a name check found several German e-invoicing products, a company and a reported Swiss word mark already using it. I renamed the library to Raitung before the first release.

_Raitung_ is an old Upper German word for rendering accounts: the statement of income and expenses that anyone managing someone else's money had to present. It comes from _raiten_, "to calculate, to render an account", and survives in Austrian administrative history. An invoice is a small Raitung.

A few things I set up before going public:

- CI on Linux and Windows. I don't own a Windows machine, and line endings alone can break byte-for-byte tests.
- Releases through nuget.org Trusted Publishing: the workflow gets a short-lived key through OpenID Connect, so no API key is stored in GitHub. Every publish waits for my approval in a protected environment.
- `AGENTS.md` with the rules a coding agent would otherwise get wrong, and a pull request template that asks contributors whether and how they used AI. AI-assisted contributions are welcome. A contributor must understand and test what they submit.

I built Raitung with Claude Code as a pair programmer.

## Try it

Install the core package from [nuget.org](https://www.nuget.org/packages/Raitung):

```bash
dotnet add package Raitung --prerelease
```

This is an alpha, and some things are missing:

- The CEN EN 16931 rules are licensed under EUPL-1.2, so they aren't bundled. You download them once for XRechnung validation. The README shows how.
- The Peppol BIS rules aren't implemented yet.
- Terms of the XRechnung extension are read but not written.
- A validation result is not a legal or tax assessment. For XRechnung, the official KoSIT validator remains the reference.

The code, the design decisions and the release notes are on [GitHub](https://github.com/dariuszparys/raitung). If you generate or receive e-invoices in .NET, I'd like to hear what works and what doesn't. Open an issue, ideally with a sample invoice that has invented data.
