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
<PackageReference Include="SlickDevTools.AspNetCore.Converters.XmlToJson" Version="3.0.1" />
<PackageVersion Include="SlickDevTools.AspNetCore.Converters.XmlToJson" Version="3.0.1" />
<PackageReference Include="SlickDevTools.AspNetCore.Converters.XmlToJson" />
paket add SlickDevTools.AspNetCore.Converters.XmlToJson --version 3.0.1
#r "nuget: SlickDevTools.AspNetCore.Converters.XmlToJson, 3.0.1"
#:package SlickDevTools.AspNetCore.Converters.XmlToJson@3.0.1
#addin nuget:?package=SlickDevTools.AspNetCore.Converters.XmlToJson&version=3.0.1
#tool nuget:?package=SlickDevTools.AspNetCore.Converters.XmlToJson&version=3.0.1
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
XmlToJsonConverterOptionsto 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
Instantiate
XmlToJsonConverterServiceclass:var converterService = new XmlToJsonConverterService();Convert XML to JSON:
string xml = "<root><key>value</key></root>"; var result = converterService.Convert(xml);Access the Converted JSON String:
string json = result.Json; Console.WriteLine(json);
Advanced Instantiation and Conversion with Options
Instantiate
XmlToJsonConverterServiceclass with options:var options = new XmlToJsonConverterOptions { PreserveEmptyElements = true }; var service = new XmlToJsonConverterService(options);Convert XML to JSON:
string xml = @" <product> <id>1</id> <name>Product 1</name> <price>100</price> </product>"; var result = service.Convert(xml);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
XmlToJsonConverterResultobject.
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 | Versions 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. |
-
net8.0
- FluentAssertions (>= 6.12.2)
- Newtonsoft.Json (>= 13.0.3)
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 |