XmlMapper.Fluent
1.0.0
dotnet add package XmlMapper.Fluent --version 1.0.0
NuGet\Install-Package XmlMapper.Fluent -Version 1.0.0
<PackageReference Include="XmlMapper.Fluent" Version="1.0.0" />
<PackageVersion Include="XmlMapper.Fluent" Version="1.0.0" />
<PackageReference Include="XmlMapper.Fluent" />
paket add XmlMapper.Fluent --version 1.0.0
#r "nuget: XmlMapper.Fluent, 1.0.0"
#:package XmlMapper.Fluent@1.0.0
#addin nuget:?package=XmlMapper.Fluent&version=1.0.0
#tool nuget:?package=XmlMapper.Fluent&version=1.0.0
XmlMapper
XmlMapper is a library for convenient mapping of data from XML to C# classes using XPath selectors. It supports flexible mapping configuration, including value transformations and nested objects.
Installation
First, add the project as a dependency in your application.
# Assumed command for installation (e.g., via NuGet)
dotnet add package XmlMapper.Fluent
Quick Start
The library allows you to configure the mapping between XML data and C# classes through MappingConfigurationBuilder, specifying the mapping of properties and nested objects.
Preparing Data and Entities
- We receive data in XML format as follows:
<LibraryContext>
<Book>
<Title>The Great Gatsby</Title>
<Author>F. Scott Fitzgerald</Author>
<Year>1925</Year>
<Synopsis>A story about the jazz age.</Synopsis>
<Genres>
<Genre name="Novel"/>
<Genre name="Fiction"/>
</Genres>
</Book>
</LibraryContext>
- Let's declare the entities in code so that we can work with the data:
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
public string Synopsis { get; set; }
public List<Genre> Genres { get; set; }
}
public class Genre
{
public string Name { get; set; }
}
Mapping Configuration
To link XML with classes, use MappingConfigurationBuilder:
var configBuilder = new MappingConfigurationBuilder();
configBuilder
.AddClassConfiguration<Book>("/LibraryContext/Book", classMap =>
{
classMap
.ForProperty(b => b.Title, "Title")
.ForProperty(b => b.Author, "Author")
.ForProperty(b => b.Year, "Year", year => year + 1) // value transformation
.ForProperty(b => b.Synopsis, "Synopsis", syn => "Synopsis: " + syn)
.ForLinkedProperty(b => b.Genres); // mapping for nested list
})
.AddClassConfiguration<Genre>("/LibraryContext/Book/Genres/Genre", classMap =>
{
classMap
.ForProperty(g => g.Name, "@name");
});
var mappingConfig = configBuilder.Build();
Mapping XML Data to Object
After configuration, you can call the MapToObject method to get an instance of Book with data filled from XML:
var xmlMapper = XmlMapperFactory.DefaultXmlMapper;
Book book = xmlMapper.MapToObject<Book>(mappingConfig, xmlString);
Usage Examples
Simple Mapping Without Transformation
For basic property mapping, use ForProperty:
configBuilder.AddClassConfiguration<Book>("/LibraryContext/Book", classMap =>
{
classMap
.ForProperty(b => b.Title, "Title")
.ForProperty(b => b.Author, "Author")
.ForProperty(b => b.Year, "Year")
.ForProperty(b => b.Synopsis, "Synopsis");
});
Mapping with Value Transformation
To perform value transformation before mapping, pass a transformation function:
configBuilder.AddClassConfiguration<Book>("/LibraryContext/Book", classMap =>
{
classMap
.ForProperty(b => b.Year, "Year", year => year + 1) // adds 1 to the year
.ForProperty(b => b.Synopsis, "Synopsis", syn => syn.ToUpper()); // converts to uppercase
});
Mapping Nested Objects
To map nested objects, use ForLinkedProperty:
configBuilder.AddClassConfiguration<Book>("/LibraryContext/Book", classMap =>
{
classMap
.ForLinkedProperty(b => b.Genres); // reference to another class
});
configBuilder.AddClassConfiguration<Genre>("/LibraryContext/Book/Genres/Genre", classMap =>
{
classMap
.ForProperty(g => g.Name, "@name");
});
Support for Object Lists
The library supports mapping to lists of objects:
List<Book> books = xmlMapper.MapToCollection<Book>(mappingConfig, xmlString);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
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 |
|---|---|---|
| 1.0.0 | 178 | 10/31/2024 |