LazySerializer 1.1.0

dotnet add package LazySerializer --version 1.1.0                
NuGet\Install-Package LazySerializer -Version 1.1.0                
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="LazySerializer" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LazySerializer --version 1.1.0                
#r "nuget: LazySerializer, 1.1.0"                
#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.
// Install LazySerializer as a Cake Addin
#addin nuget:?package=LazySerializer&version=1.1.0

// Install LazySerializer as a Cake Tool
#tool nuget:?package=LazySerializer&version=1.1.0                

LazySerializer

Maintenance GitHub license

A small library to easily serialize or deserialize an object in an XML file.

Installation

Use NuGet (LazySerializer) !

Packet manager:

PM> NuGet\Install-Package LazySerializer -Version 1.1.0

.NET CLI:

> dotnet add package LazySerializer -Version 1.1.0

Paket CLI:

> paket add LazySerializer -Version 1.1.0

Usage

Here is the object we will use for the serialization:

public class MySettings
{
   public string ConnectionName { get; set; }
   public bool Enabled { get; set; }
   public string Username { get; set; }
   public string Password { get; set; }
   public string Server { get; set; }
   public int Port { get; set; }
   public List<string> Paths;
}

We will save the object once to build the structure of the XML file. This will allow us to modify the object manually without worrying if the XML is valid or not. We will remove this code later when the file is created.

// Build Settings
MySettings settings = new MySettings
{
   ConnectionName = "Production server",
   Enabled = true,
   Username = "admin",
   Password = "123456789",
   Server = "prod.server.com",
   Port = 8080,
   Paths = new List<string> {@"C:\Temp", @"\\quality.server.com\shared$"}
};

// Save
Serializer.Write("app.settings.xml", settings);
// or
settings.Serialize("app.settings.xml");

And.. ..here is the Settings file:

<?xml version="1.0" encoding="utf-8"?>
<MySettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Paths>
   <string>C:\Temp</string>
   <string>\\quality.server.com\shared$</string>
 </Paths>
 <ConnectionName>Production server</ConnectionName>
 <Enabled>true</Enabled>
 <Username>admin</Username>
 <Password>123456789</Password>
 <Server>prod.server.com</Server>
 <Port>8080</Port>
</MySettings>

Now to load the Settings we will use the Load method:

MySettings mySettings = Serializer.Read<MySettings>("app.settings.xml");

// We can use the settings
// Server server = new Server(mySettings.Server, mySettings.Port);
// ...

A simple serialization process

No secret, this library uses the following .NET methods to serialize and deserialize:

Here is what it looks like if you want to copy/past:

Serialize

string path = "myfile.xml";
YourObject obj = new YourObject();

// ...

XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
using (StreamWriter streamWriter = new StreamWriter(path))
{
   xmlSerializer.Serialize(streamWriter, obj);
}

Deserialize

string path = "myfile.xml";
YourObject obj;

XmlSerializer xmlSerializer = new XmlSerializer(typeof(YourObject));
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
   YourObject = (YourObject)xmlSerializer.Deserialize(fileStream);
}

Limitations

Based on the MSDN, here is the limitation of the Serialization :

You must use:

  • Public properties (read and write) & fields
  • Public constructor (without args)
  • No dictionnary
  • All types to be known
  • ...

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
.NET Framework net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net6.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.1.0 404 10/31/2022