You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP

FileConversionLibrary

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

FileConversionLibrary

FileConversionLibrary is a .NET library for converting files between different formats. It supports converting CSV files to XML, PDF, Word, JSON, and YAML formats, as well as converting XML files to CSV, PDF, Word, JSON, and YAML formats. The library provides asynchronous methods for all conversions and includes comprehensive error handling.

1.5.0
Version published
Maintainers
1
Created

File Conversion Library

Image 1

A powerful .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!

New Features in v1.5.0

  • Stream API: Convert data directly from streams without temporary files (Currently In Testing Mode)
  • In-Memory Conversion: Work with data objects directly in memory (Currently In Testing Mode)
  • Advanced Options: Comprehensive configuration options for all formats
  • Enhanced Performance: Optimized for large datasets
  • Type-Safe Configuration: Strongly-typed options classes

Usage

Basic File Conversion (Original API)

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

CSV Conversions

        // Convert CSV to PDF
        await fileConverter.ConvertCsvToPdfAsync(
            @"C:\Users\User\Desktop\csv_input.csv",
            @"C:\Users\User\Desktop\output1.pdf"
        );

        // Convert CSV to JSON
        await fileConverter.ConvertCsvToJsonAsync(
            @"C:\Users\User\Desktop\csv_input.csv",
            @"C:\Users\User\Desktop\output1.json"
        );

        // Convert CSV to Word
        await fileConverter.ConvertCsvToWordAsync(
            @"C:\Users\User\Desktop\csv_input.csv",
            @"C:\Users\User\Desktop\output1.docx"
        );

        // Convert CSV to XML
        await fileConverter.ConvertCsvToXmlAsync(
            @"C:\Users\User\Desktop\csv_input.csv",
            @"C:\Users\User\Desktop\output1.xml");

        // Convert CSV to YAML
        await fileConverter.ConvertCsvToYamlAsync(
            @"C:\Users\User\Desktop\csv_input.csv",
            @"C:\Users\User\Desktop\output1.yaml"
        );

XML Conversions

        // Convert XML to CSV
        await fileConverter.ConvertXmlToCsvAsync(
            @"C:\Users\User\Desktop\xml_input.xml",
            @"C:\Users\User\Desktop\output2.csv"
        );

        // Convert XML to JSON
        await fileConverter.ConvertXmlToJsonAsync(
            @"C:\Users\User\Desktop\xml_input.xml",
            @"C:\Users\User\Desktop\output2.json"
        );

        // Convert XML to PDF
        await fileConverter.ConvertXmlToPdfAsync(
            @"C:\Users\User\Desktop\xml_input.xml",
            @"C:\Users\User\Desktop\output2.pdf"
        );

        // Convert XML to Word
        await fileConverter.ConvertXmlToWordAsync(
            @"C:\Users\User\Desktop\xml_input.xml",
            @"C:\Users\User\Desktop\output2.docx"
        );

        // Convert XML to YAML
        await fileConverter.ConvertXmlToYamlAsync(
            @"C:\Users\User\Desktop\xml_input.xml",
            @"C:\Users\User\Desktop\output2.yaml"
        );

Stream API

For web applications, cloud services, and scenarios where you work with streams:

var fileConverter = new FileConverter();

// Convert from stream to stream
using var inputStream = File.OpenRead("input.csv");
var options = new ConversionOptions 
{ 
    SourceFormat = "csv", 
    TargetFormat = "json" 
};

using var outputStream = await fileConverter.ConvertStreamAsync(inputStream, options);

// Convert stream to bytes (for HTTP responses)
var pdfBytes = await fileConverter.ConvertStreamToBytesAsync(inputStream, new ConversionOptions 
{ 
    SourceFormat = "csv", 
    TargetFormat = "pdf" 
});

// Convert stream to string
var jsonString = await fileConverter.ConvertStreamToStringAsync(inputStream, new ConversionOptions 
{ 
    SourceFormat = "csv", 
    TargetFormat = "json" 
});

Web API Example

[HttpPost("convert")]
public async Task<IActionResult> ConvertFile(IFormFile file, string targetFormat)
{
    var options = new ConversionOptions 
    { 
        SourceFormat = "csv", 
        TargetFormat = targetFormat 
    };
    
    using var inputStream = file.OpenReadStream();
    var result = await fileConverter.ConvertStreamToBytesAsync(inputStream, options);
    
    return File(result, GetMimeType(targetFormat), $"converted.{targetFormat}");
}

💾 In-Memory API

Work directly with data objects for maximum performance and flexibility:

CSV In-Memory Conversions

var fileConverter = new FileConverter();

// Create CSV data in memory
{ 
    Headers = new[] { "Name", "Age", "City" },
    Rows = new List<string[]> 
    {
        new[] { "John Doe", "25", "New York" },
        new[] { "Jane Smith", "30", "London" }
    }
};

// Convert to different formats with advanced options
var jsonOptions = new JsonConversionOptions 
{ 
    ConvertValues = true,
    UseIndentation = true,
    IncludeRowNumbers = true,
    CreateNestedObjects = true,
    NestedSeparator = ".",
    ConvertArrays = true,
    ArrayDelimiter = ";"
};
var json = fileConverter.ConvertCsvToJson(csvData, jsonOptions);

var pdfOptions = new PdfConversionOptions 
{ 
    FontSize = 12f,
    Title = "Sales Report",
    IncludeTimestamp = true,
    AlternateRowColors = true,
    LandscapeOrientation = true,
    FontFamily = "Arial"
};
var pdfBytes = fileConverter.ConvertCsvToPdf(csvData, pdfOptions);

var wordOptions = new WordConversionOptions 
{ 
    UseTable = true,
    FontFamily = "Calibri",
    FontSize = 11,
    AlternateRowColors = true,
    PageOrientation = "Landscape"
};
var wordBytes = fileConverter.ConvertCsvToWord(csvData, wordOptions);

var xmlOptions = new XmlConversionOptions 
{ 
    OutputFormat = "Elements",
    UseCData = true,
    IncludeTimestamp = true,
    NamingConvention = "CamelCase",
    AddComments = true
};
var xml = fileConverter.ConvertCsvToXml(csvData, xmlOptions);

var yamlOptions = new YamlConversionOptions 
{ 
    Structure = "Dictionary",
    NamingConvention = "CamelCase",
    ConvertDataTypes = true,
    IncludeComments = true,
    SortKeys = true
};

var yaml = fileConverter.ConvertCsvToYaml(csvData, yamlOptions);

XML In-Memory Conversions


// Create XML data in memory
var xmlContent = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<products>
    <product>
        <Product>Laptop</Product>
        <Price>999.99</Price>
        <Category>Electronics</Category>
    </product>
    <product>
        <Product>Book</Product>
        <Price>29.99</Price>
        <Category>Education</Category>
    </product>
</products>";

var xmlData = new XmlData 
{ 
    Document = XDocument.Parse(xmlContent),
    RootElementName = "products",
    XmlVersion = "1.0",
    Encoding = "UTF-8"
};

// Convert to different formats with advanced options
var csvOptions = new CsvConversionOptions 
{ 
    Delimiter = ';',
    IncludeHeaders = true,
    QuoteValues = true,
    FlattenHierarchy = true,
    IncludeAttributes = true
};
var csv = fileConverter.ConvertXmlToCsv(xmlData, csvOptions);

var jsonOptions = new JsonConversionOptions 
{ 
    ConvertValues = true,
    UseIndentation = true,
    RemoveWhitespace = true
};
var json = fileConverter.ConvertXmlToJson(xmlData, jsonOptions);

var pdfOptions = new PdfConversionOptions 
{ 
    Title = "Product Catalog",
    FontSize = 10f,
    AlternateRowColors = true,
    IncludeTimestamp = true,
    HierarchicalView = true
};
var pdf = fileConverter.ConvertXmlToPdf(xmlData, pdfOptions);

var wordOptions = new WordConversionOptions 
{ 
    UseTable = true,
    FontFamily = "Calibri",
    FontSize = 11,
    FormatAsHierarchy = true,
    AlternateRowColors = true
};
var word = fileConverter.ConvertXmlToWord(xmlData, wordOptions);

var yamlOptions = new YamlConversionOptions 
{ 
    Structure = "Dictionary",
    ConvertDataTypes = true,
    IncludeRootElement = true,
    IncludeAttributes = true,
    UseCamelCase = false,
    SortKeys = true
};
var yaml = fileConverter.ConvertXmlToYaml(xmlData, yamlOptions);

Use Cases

Web Applications

// ASP.NET Core file upload and conversion
[HttpPost("upload-convert")]
public async Task<IActionResult> UploadAndConvert(IFormFile file)
{
    using var stream = file.OpenReadStream();
    var options = new ConversionOptions { SourceFormat = "csv", TargetFormat = "pdf" };
    var result = await fileConverter.ConvertStreamToBytesAsync(stream, options);
    return File(result, "application/pdf", "report.pdf");
}

Microservices

// Convert data received from another service
public async Task<string> ProcessDataFromService(HttpResponseMessage response)
{
    using var stream = await response.Content.ReadAsStreamAsync();
    var options = new ConversionOptions { SourceFormat = "xml", TargetFormat = "json" };
    return await fileConverter.ConvertStreamToStringAsync(stream, options);
}

Data Processing Pipelines

// Process data in memory without file I/O
public byte[] GenerateReport(List<DataRecord> records)
{
    var csvData = new CsvData 
    { 
        Headers = new[] { "ID", "Name", "Value" },
        Rows = records.Select(r => new[] { r.Id, r.Name, r.Value.ToString() }).ToList()
    };
    
    return fileConverter.ConvertCsvToPdf(csvData, new PdfConversionOptions 
    { 
        Title = "Data Report",
        IncludeTimestamp = true 
    });
}

Configuration Options

JsonConversionOptions

  • ConvertValues: Auto-detect and convert data types
  • UseIndentation: Pretty-print JSON output
  • IncludeRowNumbers: Add row numbers to output
  • GroupByColumn: Group data by specific column
  • CreateNestedObjects: Support for nested object structures
  • ConvertArrays: Convert delimited values to arrays

PdfConversionOptions

  • FontSize: Text font size
  • Title: Document title
  • AlternateRowColors: Zebra-striped rows
  • LandscapeOrientation: Page orientation
  • IncludeTimestamp: Add generation timestamp

WordConversionOptions

  • UseTable: Format as table vs. paragraphs
  • FontFamily & FontSize: Typography settings
  • FormatAsHierarchy: Hierarchical data representation
  • AlternateRowColors: Row styling

XmlConversionOptions

  • OutputFormat: Elements, Attributes, Mixed, or Hierarchical
  • UseCData: Wrap content in CDATA sections
  • NamingConvention: Original, CamelCase, PascalCase, or SnakeCase
  • IncludeMetadata: Add conversion metadata

YamlConversionOptions

  • Structure: Array, Dictionary, Hierarchical, or Grouped
  • ConvertDataTypes: Auto-detect data types
  • SortKeys: Alphabetically sort keys
  • IncludeComments: Add descriptive comments

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.

FAQs

Package last updated on 04 Aug 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts