Decompose Env File into XML and JSON

Decompose Env File into XML and JSON

The CATIA environment file is not formatted in a useful friendly to share format so this post will show you how to convert it into a simple class structure and then convert it into an XML or JSON format.

Classes

We will need a couple of classes to represent the environment file structure which is comprised of variables and their value(s). Note the empty constructors these are required by the xml serializer.

Environment File Class

The first class will represent the overall environment file that contains a list of Environment variables.

C#

Environment Variable Class

The environment variable class has two main constructors, the first will be used when there is a variable without any values, and the second will take a semi-colon separated string of variable values and break them down into individual values.

C#

Helper Functions

We will need two helper functions to convert the class structure to XML and JSON.

Convert Object to JSON String

The Convert Object to Json String static method will create an indented Json file structure.

C#

Convert Object to XML String

The Convert Object to XML String static method will create an XML file structure based on the Env File Class structure.

C#

Convert Environment File

The convert environment file method requires the path to the environment file and a path to an output directory where it will write the XML and JSON representations of the original environment file. The method id in three steps read in the original environment file, decompose the file into a class structure and finally write the XML and JSON files.

C#

Rebuilding the Environment File from the Class Structure

The method shown below rebuilds the environment file from the class structure the first section rebuild the env file header, this can be tailored to [your needs.

C#

Just like before we can write the environment file back out to a new file.

C#