FileConversionLibrary 1.7.0

dotnet add package FileConversionLibrary --version 1.7.0
                    
NuGet\Install-Package FileConversionLibrary -Version 1.7.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.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FileConversionLibrary" Version="1.7.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.7.0
                    
#r "nuget: FileConversionLibrary, 1.7.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.7.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.7.0
                    
Install as a Cake Addin
#tool nuget:?package=FileConversionLibrary&version=1.7.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 net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  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.7.0:
           - (Released: 2 September 2025) - Bug fixes and improved XML parsing reliability
           - Fixed a critical bug in XML parsing logic that incorrectly identified data rows in complex, nested XML files, significantly improving accuracy.
           - Enhanced the heuristic for identifying repeating data records in XML to make it more robust for various document structures.
           - Corrected an issue in the XML-to-CSV converter to ensure it uses the correctly parsed data, preventing inconsistent output.
           - Fixed a data loss bug in the XML-to-JSON converter where child elements were being deleted when a CDATA section was present.
           - General stability and reliability improvements for all XML conversion pathways.
           - Simplified the `IFileReader` interface to a single, more flexible `ReadAsync` method.
           - Refactored `CsvFileReader` and `XmlFileReader` to align with the new unified API, improving internal consistency and maintainability.
           - **API Unification:** Standardized all file-based conversion methods in `FileConverter` to accept strongly-typed options classes (e.g., `JsonConversionOptions`, `PdfConversionOptions`) instead of long parameter lists. This creates a consistent, predictable, and extensible API across all conversion types (file, stream, and in-memory).
           
           Version 1.6.0:
           - (Released: 6 August 2025) - Enhanced XML processing capabilities
           - Added robust support for complex XML structures with nested elements
           - Implemented proper handling of CDATA sections across all XML converters
           - Added support for XML comments in output formats
           - Enhanced handling of XML attributes and hierarchical structures
           - Improved error handling for malformed XML documents
           - Optimized performance for large XML documents with complex structures
           - Added new configuration options for XML conversion customization

           Version 1.5.0:
           - (Released: 4 August 2025) - Major update with new APIs and enhanced functionality.
           - Added Stream API for direct stream-to-stream conversions without temporary files (Currently In Testing Mode).
           - Introduced In-Memory conversion API for working with data objects directly (Currently In Testing Mode).
           - Added comprehensive strongly-typed options classes for all conversion types.
           - Enhanced performance and memory efficiency for large dataset processing.
           - Added support for web applications and microservices scenarios.
           - Implemented advanced configuration options for all output formats.