Crystal Kwok XD5SWy7hMbw Unsplash

Create C# Classes from a LIXI2 Schema

As a .NET developer, the first thing you will want to do when you start working with the LIXI2 schema is to generate C# classes from the XSD file. To save you some time (and get you on the real work as quickly as possible) here is a quick guide!

In this tutorial we will step through the process of:

  • Generating C# classes using dotnet-xscgen
  • Creating your first LIXI2 message using the C# classes
  • Print your C# LIXI2 object to the console
  • Writing your C# LIXI2 object as an XML file
  • Reading a LIXI2 message from XML into C# objects

Generating your LIXI C# Classes

The tool we will be using to Generate our C# classes is called dotnet-xscgen.

1) install the tool from the command line

dotnet tool install --global dotnet-xscgen --version 2.0.322

2) to check that the tool has installed:

dotnet tool list --global

3) go to the directory in which your LIXI schema file is located and execute this command:

xscgen -n =lixi LIXI-CAL-2_6_24.xsd

the generated file will be something like this:

//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// 
//------------------------------------------------------------------------------

// This code was generated by XmlSchemaClassGenerator version 2.0.0.0.
namespace lixi
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Xml.Serialization;
    
...

Creating your first LIXI Message

1) Create a new project and add the using statement:

using lixi;

2) Add a method to create a new LIXI Package

static Package CreatePackage()
{
    Package package = new Package();
    package.Publisher = new PackagePublisher();
    package.Publisher.CompanyName = "LIXI";
    package.Publisher.ContactName = "John Matthews";
    package.Publisher.Email = "john@lixi.org.au";
    return package;
}

3) Update your main to call the method

static void Main(string[] args)
{
    Package package = CreatePackage();
}

Print your LIXI object to the Console

To serialise the object to XML you will need to use the XmlSerializer. We can create the string to write to the console using the StringWriter.

1) Add a the function to print your package to the console

2) Update your main function to call the print function

3) Add the using statements required for XmlSerializer and StringWriter

using System.IO;
using System.Xml.Serialization;

static void Main(string[] args)
{
    Package package = CreatePackage();
    Print(package);
}

static void Print(Package package)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Package));
    StringWriter stringWriter = new StringWriter();
    serializer.Serialize(stringWriter, package);
    Console.WriteLine(stringWriter.ToString());
}

Writing your LIXI C# Object as an XML file

We can use the XmlTextWriter to write you LIXI message to file.

using System.Text;
using System.Xml;
using System.Xml.Serialization;

Here we can control the formatting, indentation, namespaces, and set the encoding to UTF-8.

static void Main(string[] args)
{
    Package package = CreatePackage();
    Write(package, "lixi.xml");
}

static void Write(Package package, String filename)
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(Package));
    XmlTextWriter xmlTextWriter = new XmlTextWriter(filename, Encoding.UTF8);
    xmlTextWriter.Formatting = Formatting.Indented;
    XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
    xmlSerializerNamespaces.Add(string.Empty, string.Empty);
    xmlSerializer.Serialize(xmlTextWriter, package, xmlSerializerNamespaces);
}

Reading a LIXI message from XML

To read a LIXI message and load it into our C# data model we again use the XmlSerializer.

static void Main(string[] args)
{
    Package package = Read("lixi.xml");
    Print(package);
}

static Package Read (string filename)
{
    XmlSerializer serializer = new XmlSerializer(typeof(Package));
    return (Package) serializer.Deserialize(new XmlTextReader(filename));
}

And you will see the XML displayed on the console:

<?xml version="1.0" encoding="utf-16"?>
<Package>
  <Publisher CompanyName="LIXI" ContactName="John Matthews" Email="john@lixi.org.au" />
</Package>

Related Posts...

We've published similar blog posts showing how to generate Python classes here and JAVA Classes here.


Written by:
John Matthews, LIXI Technical Lead
First Published: February 5, 2020