Universal Business Language (UBL) is a royalty-free model of business document information in XML syntax. UBL was developed by Organization for the Advancement of Structured Information Standards (OASIS) to be as an entry point to electronic commerce for small and medium-sized business.
If you have to read or write XML documents compliant with the UBL standard, xsd2code++ can help you and save you a lot of time.
So if you want to :
We are going to see in this article how to read and write an invoice with the code generated by xsd2code. In this article we are going to use the 2.1 version of UBL with an example code which is realized under .NET5.
The first step is to create a console project in Visual Studio and add the UBL schemas.
Then, to generate the code, choose the XML schema corresponding to the document you want to produce, in this example it is the invoice. A simple right click on the schema allows you to access the code generator settings:
The important options are the activation of XML attributes and the read and write method of XML serialization
Avoid circular references ! UBL schemas contains circular references. To avoid a StackOverFlow exception, InitializeFields must be at "none" or "collection". The complex types will not be created which avoids the circular reference.
Just in case you didn't know, this will generate a class with attributes based on the xml, JSon, BSon or Messagepack data file or schema, with serialization enabled you will have the serialization and deserialization functions in the class depending on which formats you've enabled.
From there depending on what your needs are you can, through c# and the generated functions, code your program to generate the files you need in the right way following the class that has been generated from either the schema or a data file, thanks to Xsd2Code you can stick with the Xml and switch to or add other output formats like JSon.
A sample implementation is available on GitHUB. We provide a set of examples based on schemas and standards such as FpML or UBL.