dotnet-core/samples/xmlserializergenerator-instructions.md
2020-10-05 12:29:00 -07:00

2.1 KiB

Using Xml Serializer Generator on .NET Core

Like the Xml Serializer Generator (sgen.exe) on desktop, Microsoft.XmlSerializer.Generator NuGet package is the solution for .NET Core and .NET Standard Libraries. It creates an Xml serialization assembly for types contained in an assembly to improve the startup performance of Xml serialization when serializing or de-serializing objects of those types using XmlSerializer.

You can start using the tool today following the instructions below.

Prerequisites

The following is required for Microsoft.XmlSerializer.Generator to work. You can use command dotnet --info to check which versions of .NET Core SDK and runtime you may already have installed.

Instructions

Here are the step by step instructions on how to use Xml Serializer Generator in a .NET Core console application.

  1. Create a .NET Core console application, e.g. create an app named 'MyApp' with the command,

    dotnet new console --name MyApp
    
  2. Edit the .csproj and add a reference to the Microsoft.XmlSerializer.Generator package. For example,

    1. Run command: dotnet add package Microsoft.XmlSerializer.Generator -v 1.0.0

    2. Add the following lines in MyApp.csproj,

    <ItemGroup>
      <DotNetCliToolReference Include="Microsoft.XmlSerializer.Generator" Version="1.0.0" />
    </ItemGroup>
    
  3. Add a class in the application. For example, add the class below in Program.cs,

    public class MyClass
    {
        public int Value;
    }
    

    Now you can create an XmlSerializer for MyClass.

    var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
    
  4. Build the application by running dotnet build. If everything succeeds, an assembly named MyApp.XmlSerializers.dll will be generated in the output folder. You will see warnings in the build output if the tool failed to generate the assembly.

Start the application and it will automatically load and use the pre-generated serializers at runtime.