Try it yourself with our free Toml Formatter tool — runs entirely in your browser, no signup needed.

How to Parse TOML in C#

How to Parse TOML in C#

Parsing TOML (Tom's Obvious, Minimal Language) configuration files is a common task in C# development. TOML is a lightweight, human-readable format for storing configuration data, making it a popular choice for many applications. In this guide, we will walk through the process of parsing TOML in C# using the popular Toml.Net library.

Installing the Toml.Net Library

To start, you'll need to install the Toml.Net NuGet package. You can do this using the NuGet Package Manager Console:

Install-Package Toml.Net

Alternatively, you can install it using the .NET CLI:

dotnet add package Toml.Net

Quick Example

Here is a minimal example of how to parse a TOML file in C#:

using Toml;

class TomlParser
{
    public static void Main(string[] args)
    {
        string tomlContent = File.ReadAllText("example.toml");
        TomlTable table = Toml.Parse(tomlContent);
        Console.WriteLine(table["title"]); // prints "Example TOML File"
    }
}

This code reads a TOML file named example.toml and parses its contents into a TomlTable object. It then accesses the value of the title key and prints it to the console.

Step-by-Step Breakdown

Let's walk through the code line by line:

  1. using Toml;: We import the Toml namespace, which contains the classes and methods for working with TOML.
  2. string tomlContent = File.ReadAllText("example.toml");: We read the contents of the example.toml file into a string using the File.ReadAllText method.
  3. TomlTable table = Toml.Parse(tomlContent);: We pass the TOML content to the Toml.Parse method, which returns a TomlTable object representing the parsed TOML data.
  4. Console.WriteLine(table["title"]);: We access the value of the title key in the TomlTable object using the indexer syntax (table["title"]). We then print the value to the console using Console.WriteLine.

Handling Edge Cases

Here are some common edge cases to consider when parsing TOML in C#:

Empty/Null Input

If the input TOML content is empty or null, the Toml.Parse method will throw a FormatException. You can handle this by checking for null or empty input before parsing:

if (string.IsNullOrEmpty(tomlContent))
{
    Console.WriteLine("Error: empty or null input");
    return;
}

Invalid Input

If the input TOML content is invalid (e.g., contains syntax errors), the Toml.Parse method will throw a FormatException. You can handle this by catching the exception and logging an error message:

try
{
    TomlTable table = Toml.Parse(tomlContent);
    // ...
}
catch (FormatException ex)
{
    Console.WriteLine($"Error: invalid input - {ex.Message}");
}

Large Input

If the input TOML content is very large, parsing it may consume a significant amount of memory. To mitigate this, you can use the Toml.ParseAsync method to parse the TOML content asynchronously:

await Toml.ParseAsync(tomlContent);

Unicode/Special Characters

TOML supports Unicode characters, but some special characters (e.g., carriage returns, line feeds) may be interpreted incorrectly. To handle this, you can use the Toml.Parse method with the encoding parameter set to Encoding.UTF8:

TomlTable table = Toml.Parse(tomlContent, Encoding.UTF8);

Common Mistakes

Here are three common mistakes developers make when parsing TOML in C#:

Mistake 1: Not Checking for Null or Empty Input

// Wrong code
TomlTable table = Toml.Parse(tomlContent);

// Corrected code
if (string.IsNullOrEmpty(tomlContent))
{
    Console.WriteLine("Error: empty or null input");
    return;
}
TomlTable table = Toml.Parse(tomlContent);

Mistake 2: Not Handling Invalid Input

// Wrong code
TomlTable table = Toml.Parse(tomlContent);

// Corrected code
try
{
    TomlTable table = Toml.Parse(tomlContent);
    // ...
}
catch (FormatException ex)
{
    Console.WriteLine($"Error: invalid input - {ex.Message}");
}

Mistake 3: Not Handling Large Input

// Wrong code
TomlTable table = Toml.Parse(tomlContent);

// Corrected code
await Toml.ParseAsync(tomlContent);

Performance Tips

Here are three performance tips for parsing TOML in C#:

  1. Use asynchronous parsing: As mentioned earlier, parsing large TOML content can consume a significant amount of memory. Using asynchronous parsing can help mitigate this issue.
  2. Use caching: If you need to parse the same TOML content multiple times, consider caching the parsed result to avoid redundant parsing.
  3. Use a streaming parser: If you need to parse very large TOML content, consider using a streaming parser that can parse the content in chunks, rather than loading the entire content into memory at once.

FAQ

Q: What is the difference between Toml.Parse and Toml.ParseAsync?

A: Toml.Parse is a synchronous method that parses the TOML content immediately, while Toml.ParseAsync is an asynchronous method that parses the TOML content asynchronously.

Q: How do I handle invalid TOML input?

A: You can handle invalid TOML input by catching the FormatException exception thrown by the Toml.Parse method.

Q: Can I use TOML with Unicode characters?

A: Yes, TOML supports Unicode characters. However, some special characters (e.g., carriage returns, line feeds) may be interpreted incorrectly.

Q: How do I cache parsed TOML content?

A: You can cache parsed TOML content by storing the parsed result in a cache store (e.g., MemoryCache) and reusing it instead of re-parsing the content.

Q: What is the performance impact of using asynchronous parsing?

A: Asynchronous parsing can improve performance by avoiding blocking the calling thread while parsing large TOML content. However, it may also introduce additional overhead due to the asynchronous operation.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp