SlickDevTools.AspNetCore.Converters.XmlToJson 3.0.1

dotnet add package SlickDevTools.AspNetCore.Converters.XmlToJson --version 3.0.1
                    
NuGet\Install-Package SlickDevTools.AspNetCore.Converters.XmlToJson -Version 3.0.1
                    
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="SlickDevTools.AspNetCore.Converters.XmlToJson" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SlickDevTools.AspNetCore.Converters.XmlToJson" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SlickDevTools.AspNetCore.Converters.XmlToJson" />
                    
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 SlickDevTools.AspNetCore.Converters.XmlToJson --version 3.0.1
                    
#r "nuget: SlickDevTools.AspNetCore.Converters.XmlToJson, 3.0.1"
                    
#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 SlickDevTools.AspNetCore.Converters.XmlToJson@3.0.1
                    
#: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=SlickDevTools.AspNetCore.Converters.XmlToJson&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SlickDevTools.AspNetCore.Converters.XmlToJson&version=3.0.1
                    
Install as a Cake Tool

SlickDevTools.AspNetCore.Converters.XmlToJson

A robust and high-performance library for converting XML data to JSON format in C#. Version 3.0 brings significant performance improvements and new features.

What's New in Version 3.0

  • NEW: Smart Array Detection - Automatically detects and converts repeated XML elements into JSON arrays
  • NEW: XML Attributes Support - Optionally include XML attributes in JSON output with @ prefix
  • FIXED: O(n²) performance issue - Now uses single-pass algorithm for 2-10x faster conversion
  • NEW: Empty Element Handling - Configurable preservation of empty XML elements as null values
  • IMPROVED: Hierarchical Parsing - Enhanced recursive parsing that accurately preserves nested XML structures

Features

  • Easy-to-use XML to JSON Conversion: Simplifies the process of converting XML data to JSON format.
  • Customizable Options: Use XmlToJsonConverterOptions to tailor the conversion process.
  • Structured Result Output: Returns conversion results encapsulated in XmlToJsonConverterResult.

Getting Started

Prerequisites

Ensure you have the following installed:

Installation

To install the SlickDevTools.AspNetCore.Converters.XmlToJson library in your project, add the following dependency to your project file:

<PackageReference Include="SlickDevTools.AspNetCore.Converters.XmlToJson" Version="3.0.0" />

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

dotnet add package SlickDevTools.AspNetCore.Converters.XmlToJson --version 3.0.0

Usage

Basic Instantiation and Conversion

  1. Instantiate XmlToJsonConverterService class:

    var converterService = new XmlToJsonConverterService();
    
  2. Convert XML to JSON:

    string xml = "<root><key>value</key></root>";
    var result = converterService.Convert(xml);
    
  3. Access the Converted JSON String:

    string json = result.Json;
    Console.WriteLine(json);
    

Advanced Instantiation and Conversion with Options

  1. Instantiate XmlToJsonConverterService class with options:

    var options = new XmlToJsonConverterOptions { PreserveEmptyElements = true };
    var service = new XmlToJsonConverterService(options);
    
  2. Convert XML to JSON:

    string xml = @"
        <product>
            <id>1</id>
            <name>Product 1</name>
            <price>100</price>
        </product>";
    var result = service.Convert(xml);
    
  3. Access the Converted JSON String:

    string jsonString = result.Json;
    Console.WriteLine(jsonString);
    

Component Overview

XmlToJsonConverterService

The main class responsible for handling the conversion process. It provides a constructor that accepts an optional XmlToJsonConverterOptions object for customization. Key method:

  • Convert: Converts an XML string into a JSON string and returns it encapsulated within a XmlToJsonConverterResult object.

XmlToJsonConverterOptions

A class designed to specify options for the conversion process. Available options:

  • PreserveEmptyElements: Determines whether empty XML elements should be preserved as null values in JSON (default: false)
  • IgnoreAttributes: Whether to ignore XML attributes during conversion (default: true). When false, attributes are included with @ prefix

XmlToJsonConverterResult

Represents the result of the XML to JSON conversion process. It contains:

  • Json: A string property containing the resulting JSON.

Advanced Features

Array Detection (NEW in 3.0)

Repeated XML elements are automatically converted to JSON arrays:

<root>
  <item>1</item>
  <item>2</item>
  <item>3</item>
</root>

Converts to:

{
  "root": {
    "item": ["1", "2", "3"]
  }
}

XML Attributes (NEW in 3.0)

Include XML attributes in JSON output:

var options = new XmlToJsonConverterOptions { IgnoreAttributes = false };
var service = new XmlToJsonConverterService(options);
var xml = "<person id=\"123\" name=\"John\">Engineer</person>";
var result = service.Convert(xml);

Output:

{
  "person": {
    "@id": "123",
    "@name": "John",
    "#text": "Engineer"
  }
}

Empty Element Handling

var options = new XmlToJsonConverterOptions { PreserveEmptyElements = true };
var service = new XmlToJsonConverterService(options);
var xml = "<root><empty></empty></root>";
var result = service.Convert(xml);
// Output: {"root": {"empty": null}}

Performance

Version 3.0 includes significant performance improvements:

  • Single-pass algorithm: Reduced O(n²) to O(n) complexity
  • 2-10x faster: Especially noticeable on deeply nested documents
  • Memory efficient: Optimized for large XML documents

Dependencies

  • Newtonsoft.Json: JSON manipulation
  • FluentAssertions: Debug-mode assertions
  • System.Xml: XML parsing

License

This project is released under the MIT License. Feel free to use, modify, and distribute this software.

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 Downloads Last Updated
3.0.1 196 10/21/2025