Home  /  Blog  /  5 reasons to use XSD2Code

5 reasons to use XSD2Code

March 2021

There are many tools out there that can be used for the generation of JSon and or XML related coding in many languages wether it be generating classes from a schema or a document or the other way around generating schemas based on classes or objects

In this short article we will go over some of the reasons why you should be using Xsd2Code, it's main features and properties that put it above it's competitors.

1 - Lazy Loading

Xsd2code not only has quality, easily readable code it also has many other features, it does not simply create your class in C# it also handles things like lazy generation.

Lazy initialization/generation/Loading of an object means that its creation is deferred until it is first used. Lazy initialization is primarily used to improve performance, avoid wasteful computation, and reduce program memory requirements. These are the most common scenarios:

- When you have an object that is expensive to create, and the program might not use it. If the user never asks to display or use the data in a computation, then there is no reason to use system memory or computing cycles to create it. By lazily initializing, you can avoid wasting system resources when the object is not used.

- When you have an object that is expensive to create, and you want to defer its creation until after other expensive operations have been completed.

You can enable lazy loading in the Output property settings like shown :
Images showing where to enable the lazy loading setting
Here's what it will look like before you turn Lazy Loading On :
 Image showing the generated class without lazy loading
And this is what it will look like after : 
Image showing the generated class with lazy loading on
Lazy pattern is used when it's possible, and now It initializes complex types when the property is accessed

2 - Simplicity, depth and serialization

With Xsd2Code you can handle almost anything. If you're just looking to turn your schema into a class you can, but if you take a look at the veritable treasure trove of options you might not only be blown away but also learn new things, and although it may seem scary at first most of the settings are simple switches to have things on or off. Xsd2Code is able to gracefully combine simplicity of use and depth.

For example with just a click you have serialization and deserialization methods generated for you in your resulting class and these functions will adapt to the schema being used by XSD2Code.

Here's how to enable this setting and what it will look like :
Image showing how to enable serialization
And these are the various functions that will be generated for you : Image showing the various functions to serialize and deserialize
You can see these functions and the processes of serialization and deserialization here.

3 - Productivity

Xsd2Code is not only easy to install, as you can simlpy integrate it NuGet package into your VisualStudio (wether it be the Pro, Ent, community 2019, 2017 or older versions), once impleted with only a few clicks you can have your various classes generated in C# with many options. To add to this productivity gain, the serialization code is automatically generated so you don't have to worry about it, but on top of generating this code for you, XSD2Code can do it in multiple formats, have a look :

First as seen before you have to enable serialization in the settings, precisely in the Output advanced code settings section as shown here :    Image showing how to enable serialization
You may notice the different options, depending on what you need you could switch from BSon, JSon to XML or even to MessagePack. For now you have to choose, but you will soon be able to have XSD2Code handle all of the formats.

Here's what the Deserialize function will look like depending on the chosen format :

- JSon :
Image showing the Serialize function for JSon
- BSon :
Image showing the Serialization function for BSon
- MessagePack :
Image showing the Serialize fucntion for the MessagePack
- XML :
Image showing the Serialize function for XML

4 - Integration and ease of use

Xsd2Code also has tight VS integration, including context menu integration and a custom build tool (which means that if you reference the XSDs in your project and specify the custom tool, it will automatically update the code as you update the XSD).
You can see in our installation or quickstart pages this advantages in effect

5 - Versatility

To add to the expansive list of options, now, thanks to our recent update, you are able to generate the code needed to serialize in all available formats, so JSon, BSon, MessagePack and XML, all you need to do is look at our new setting to activate additional serializers.

You'll find this in the Output advanced code settings section, as shown here :

Image showing where to enable Additional serializers

You can select the different serializers you want to have in your generated classes, and the various serializers will look like this.

Image showing different serializer functions next to one another

Here's a free tip, consider using messagepack if size and storage is important to you, this compact binary JSon format can help you.