FileConversionLibrary 1.8.0

dotnet add package FileConversionLibrary --version 1.8.0
                    
NuGet\Install-Package FileConversionLibrary -Version 1.8.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FileConversionLibrary" Version="1.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FileConversionLibrary" Version="1.8.0" />
                    
Directory.Packages.props
<PackageReference Include="FileConversionLibrary" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FileConversionLibrary --version 1.8.0
                    
#r "nuget: FileConversionLibrary, 1.8.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FileConversionLibrary@1.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FileConversionLibrary&version=1.8.0
                    
Install as a Cake Addin
#tool nuget:?package=FileConversionLibrary&version=1.8.0
                    
Install as a Cake Tool

File Conversion Library

File Conversion Library Showcase

A .NET library for converting CSV and XML files to various formats including XML, PDF, Word, JSON, and YAML. Now with enhanced Stream API and In-Memory conversion capabilities!

Key Features

  • Unified API: A consistent and predictable API across file, stream, and in-memory conversions using strongly-typed options classes.
  • Robust Parsing: Advanced heuristics to reliably parse complex and real-world CSV and XML files.
  • Multiple Conversion Modes:
    • File-Based: Convert files directly from disk.
    • Stream-Based: For web applications, microservices, and data pipelines.
    • In-Memory: High-performance, low-overhead conversions on data objects.
  • Extensive Customization: Fine-tune every aspect of the output with detailed, format-specific options.

Usage

1. File-Based Conversion

The most straightforward way to use the library. All methods now accept a dedicated options class for easy configuration.

// Create a single instance of the converter
var fileConverter = new FileConverter();

// --- Example 1: CSV to PDF with custom options ---
await fileConverter.ConvertCsvToPdfAsync(
    "input.csv",
    "output.pdf",
    new PdfConversionOptions { Title = "Sales Report", AlternateRowColors = true }
);

// --- Example 2: XML to JSON with custom options ---
await fileConverter.ConvertXmlToJsonAsync(
    "input.xml",
    "output.json",
    new JsonConversionOptions { UseIndentation = true, ConvertValues = true }
);

2. Stream-Based Conversion

Perfect for web applications or data pipelines where you need to process data without saving it to disk.

// Convert a CSV stream to a PDF byte array for an HTTP response
[HttpPost("convert")]
public async Task<IActionResult> ConvertFile(IFormFile file)
{
    using var inputStream = file.OpenReadStream();
    
    var pdfBytes = await fileConverter.ConvertStreamToBytesAsync(inputStream, 
        new PdfConversionOptions { 
            SourceFormat = "csv", 
            TargetFormat = "pdf",
            Title = "Uploaded Report"
        });
    
    return File(pdfBytes, "application/pdf", "converted_report.pdf");
}

3. In-Memory Conversion

For maximum performance and flexibility, work directly with data objects.

From CSV Data
// 1. Create your CsvData object
var csvData = new CsvData 
{ 
    Headers = new[] { "Name", "Age", "City" },
    Rows = new List<string[]> 
    {
        new[] { "John Doe", "25", "New York" },
        new[] { "Jane Smith", "30", "London" }
    }
};

// 2. Convert it to any format with detailed options
var wordBytes = fileConverter.ConvertCsvToWord(csvData, new WordConversionOptions 
{ 
    UseTable = true,
    FontFamily = "Calibri",
    AlternateRowColors = true
});

File.WriteAllBytes("in_memory_report.docx", wordBytes);
From XML Data

The library supports two ways to provide in-memory XML data:

  • For Table-Based Formats (CSV, PDF, Word): Provide pre-parsed Headers and Rows.
  • For Tree-Based Formats (JSON, YAML): Provide the full XDocument.
// --- For Table-Based output (e.g., PDF) ---
var xmlDataForTables = new XmlData
{
    Headers = new[] { "Product", "Price", "Category" },
    Rows = new List<string[]>
    {
        new[] { "Laptop", "999.99", "Electronics" },
        new[] { "Book", "29.99", "Education" }
    }
};
var pdfBytes = fileConverter.ConvertXmlToPdf(xmlDataForTables, new PdfConversionOptions { Title = "Product Catalog" });


// --- For Tree-Based output (e.g., JSON) ---
var xmlContent = @"<products><product><Name>Laptop</Name><Price>999.99</Price></product></products>";
var xmlDataForTree = new XmlData { Document = XDocument.Parse(xmlContent) };

var json = fileConverter.ConvertXmlToJson(xmlDataForTree, new JsonConversionOptions { UseIndentation = true });

Configuration Options

Customize your output by passing an options object to any conversion method.

  • JsonConversionOptions: Control indentation, data type conversion, object nesting, and more.
  • PdfConversionOptions: Set titles, font sizes, page orientation, and row styling.
  • WordConversionOptions: Generate a table or hierarchical text, set fonts, and style rows.
  • XmlConversionOptions: Define output structure (elements vs. attributes), naming conventions, and metadata.
  • YamlConversionOptions: Choose data structure, naming conventions, and data type handling.
  • CsvConversionOptions: Specify delimiters, quoting behavior, and attribute handling for XML sources.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Bohdan Harabadzhyu

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version 1.8.0: Migrated to .NET 9. Restructured into src/tests/samples layout. Central Package Management. Latest code analysis and code style enforcement. Improved null safety and ConfigureAwait correctness.