Skip to content

khanhnd157/SerializationXml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MazeNET.SerializationXml

.NET License

.NET XML Serialization Helper - Clean Architecture

✨ Features

  • βœ… Clean Architecture design pattern
  • βœ… Interface-based abstractions
  • βœ… Multi-targeting support: .NET Framework 4.8, .NET 9.0, .NET 10.0
  • βœ… Nullable reference types support for .NET 9+
  • βœ… Comprehensive XML documentation
  • βœ… Backward compatible API

πŸ—οΈ Architecture

MazeNET.SerializationXml/
β”œβ”€β”€ Core/
β”‚   β”œβ”€β”€ Interfaces/
β”‚   β”‚   β”œβ”€β”€ IXmlSerializer.cs
β”‚   β”‚   β”œβ”€β”€ IXmlFileOperations.cs
β”‚   β”‚   └── IXmlDocumentConverter.cs
β”‚   └── Options/
β”‚       β”œβ”€β”€ XmlOptions.cs
β”‚       β”œβ”€β”€ XmlDeclarationOptions.cs
β”‚       └── XmlOptionsBuilder.cs
└── Infrastructure/
    β”œβ”€β”€ Converters/
    β”‚   β”œβ”€β”€ XmlSerializerService.cs
    β”‚   └── XmlFileOperationsService.cs
    └── Extensions/
        └── XmlExtensions.cs

πŸ“¦ Installation

Install from NUGET https://www.nuget.org/packages/MazeNET.SerializationXml/

Install-Package MazeNET.SerializationXml

Or via .NET CLI:

dotnet add package MazeNET.SerializationXml

πŸš€ Usage

Include package into your project:

using MazeNET.SerializationXml;
using MazeNET.SerializationXml.Core.Interfaces;
using MazeNET.SerializationXml.Core.Options;

πŸ“– API Reference

Using Facade (Simple API - Backward Compatible)

SerializeObject

SerializeObject to XmlDocument:

var doc = XmlConverter.SerializeObject(myObject);

// -- Serialize with config
var doc = XmlConverter.SerializeObject(myObject, builder =>
          builder.RootElement("Products")
                .RemoveDeclaration()
                .RemoveTagCDDATA()
                .RemoveSchema());
      
// -- OR add options with builder function:
var doc = XmlConverter.SerializeObject(myObject).Builder(builder =>
          builder.RootElement("RootName")
                 .RemoveDeclaration()
                 .RemoveTagCDDATA()
                 .RemoveSchema());

DeserializeObject

// From XML string
var myObject = XmlConverter.DeserializeObject<MyType>(xmlString);

// From XmlDocument
var myObject = XmlConverter.DeserializeObject<MyType>(xmlDocument);

ConvertToString

// Convert XmlDocument to string
var xmlString = xmlDocument.ConvertToString();

// OR chain with serialization
var xmlString = XmlConverter.SerializeObject(myObject).ConvertToString();

Load file XML to XmlDocument:

var path = @"C:\Invoices.xml";
var doc = XmlConverter.LoadXml(path);

Load file XML to Object:

var path = @"C:\Invoices.xml";
var data = XmlConverter.FileToObject<Invoice>(path);

Save data to file XML:

// Save object to file
var path = @"C:\Invoices.xml";
XmlConverter.SaveToFile(path, myObject);

// Save XmlDocument to file
var path = @"C:\Invoices.xml";
XmlConverter.SaveToFile<Invoice>(path, xmlDocument);

Using Interfaces (Dependency Injection)

For modern applications using dependency injection:

using MazeNET.SerializationXml.Core.Interfaces;
using MazeNET.SerializationXml.Infrastructure.Converters;

// Register in your DI container
services.AddSingleton<IXmlSerializer, XmlSerializerService>();
services.AddSingleton<IXmlFileOperations, XmlFileOperationsService>();

// Use in your classes
public class MyService
{
    private readonly IXmlSerializer _xmlSerializer;
    private readonly IXmlFileOperations _fileOps;
    
    public MyService(IXmlSerializer xmlSerializer, IXmlFileOperations fileOps)
    {
        _xmlSerializer = xmlSerializer;
        _fileOps = fileOps;
    }
    
    public void SaveData(MyData data, string path)
    {
        var xmlDoc = _xmlSerializer.Serialize(data);
        _fileOps.SaveToFile(path, xmlDoc);
    }
    
    public MyData LoadData(string path)
    {
        return _fileOps.LoadFromFile<MyData>(path);
    }
}

πŸ”§ Configuration Options

XmlOptionsBuilder Methods

  • RootElement(string name) - Set root element name
  • AddDeclaration(XmlDeclarationOptions) - Add XML declaration
  • RemoveDeclaration(bool) - Remove XML declaration
  • RemoveSchema(bool) - Remove XML schema
  • RemoveTagCDDATA(bool) - Remove CDATA tags
  • AddPrefix(string) - Add XML prefix

XmlDeclarationOptions Properties

  • Version - XML version (default: "1.0")
  • Encoding - XML encoding (default: UTF-8)
  • Standalone - Standalone declaration (default: true)

πŸ”„ Migration from v1.x

If you're upgrading from CodeMazeNET.Serialization.Xml v1.x:

// Old namespace (v1.x)
using CodeMazeNET.Serialization.Xml;

// New namespace (v2.x)
using MazeNET.SerializationXml;

The API remains the same, so your existing code will work with just the namespace change!

πŸ“ Example

using MazeNET.SerializationXml;
using MazeNET.SerializationXml.Core.Options;

public class Invoice
{
    public int Id { get; set; }
    public string Customer { get; set; }
    public decimal Amount { get; set; }
}

// Serialize
var invoice = new Invoice 
{ 
    Id = 1, 
    Customer = "John Doe", 
    Amount = 150.00m 
};

var xmlDoc = XmlConverter.SerializeObject(invoice, builder => 
    builder.RootElement("Invoice")
           .RemoveSchema()
           .AddDeclaration(new XmlDeclarationOptions
           {
               Version = "1.0",
               Encoding = Encoding.UTF8,
               Standalone = true
           }));

// Save to file
XmlConverter.SaveToFile("invoice.xml", invoice);

// Load from file
var loadedInvoice = XmlConverter.FileToObject<Invoice>("invoice.xml");

// Deserialize from XML string
var xmlString = xmlDoc.ConvertToString();
var deserializedInvoice = XmlConverter.DeserializeObject<Invoice>(xmlString);

🎯 Benefits of Clean Architecture

  1. Separation of Concerns - Core business logic separated from infrastructure
  2. Testability - Easy to mock interfaces for unit testing
  3. Maintainability - Clear structure makes code easier to understand and modify
  4. Flexibility - Easy to swap implementations without changing client code
  5. Dependency Inversion - High-level modules don't depend on low-level modules

Thanks

Thanks for use, if it's helpful for you please send me 1 star! ⭐

About

.NET XML Serialization Helper

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages