Table of Contents
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.
//EnvFile.cs public class EnvFile { public EnvFile() { } public List<EnvVariable> EnvVariables { get; set; } = new List<EnvVariable>() { }; }
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.
//EnvVariable.cs public class EnvVariable { public string VariableName { get; set; } public List<string> EnvPaths { get; set; } = new List<string>() { }; public EnvVariable() { } public EnvVariable(string variableName) { VariableName = variableName; } public EnvVariable(string variableName, string envPathsArray) { VariableName = variableName; string[] separatingStrings = { ";" }; string[] envPaths = envPathsArray.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries); foreach (var path in envPaths) { EnvPaths.Add(path); } } }
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.
private static string ConvertObjectToJSONString(EnvFile classObject) { var opt = new JsonSerializerOptions() { WriteIndented = true }; return JsonSerializer.Serialize<EnvFile>(classObject, opt); }
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.
private static string ConvertObjectToXMLString(object classObject) { string xmlString = null; XmlSerializer xmlSerializer = new XmlSerializer(classObject.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { xmlSerializer.Serialize(memoryStream, classObject); memoryStream.Position = 0; xmlString = new StreamReader(memoryStream).ReadToEnd(); } return xmlString; }
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.
public static void ConvertEnvFile(string path, string outputPath) { EnvFile envFile = new EnvFile(); using var fs = new FileStream(path, FileMode.Open, FileAccess.Read); using var sr = new StreamReader(fs, Encoding.UTF8); string line = String.Empty; while ((line = sr.ReadLine()) != null) { if (line.Length > 0) { if (line.Substring(0, 1) != "!") { string[] separatingStrings = { "=" }; string[] envPaths = line.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries); if (envPaths.Length == 2) { envFile.EnvVariables.Add(new EnvVariable(envPaths[0], envPaths[1])); } else { envFile.EnvVariables.Add(new EnvVariable(envPaths[0])); } } } } string xmlString = ConvertObjectToXMLString(envFile); using (StreamWriter file = new StreamWriter(string.Concat(outputPath, "Env.xml"))) { file.Write(xmlString); } string strJson = ConvertObjectToJSONString(envFile); using (StreamWriter file = new StreamWriter(string.Concat(outputPath, "Env.json"))) { file.Write(strJson); } }
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.
private static string ConvertObjecttoEnvFileString(EnvFile classObject) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(string.Concat("!----------------------------------------------------------", Environment.NewLine)); stringBuilder.Append(string.Concat("!DASSAULT SYSTEMES - ENVIRONMENT FILE", Environment.NewLine)); stringBuilder.Append(string.Concat("!----------------------------------------------------------", Environment.NewLine)); stringBuilder.Append(string.Concat("!MODE: Global", Environment.NewLine)); stringBuilder.Append(string.Concat("!PRODUCT LINE: COMMON", Environment.NewLine)); stringBuilder.Append(string.Concat("!TMSTMP: 1605123714", Environment.NewLine)); stringBuilder.Append(string.Concat("!ARGS:", Environment.NewLine)); stringBuilder.Append(string.Concat("!MARKETING VERSION:", Environment.NewLine)); stringBuilder.Append(string.Concat("!----------------------------------------------------------", Environment.NewLine)); stringBuilder.Append(Environment.NewLine); foreach (EnvVariable envVariable in classObject.EnvVariables) { string newLine = string.Concat( envVariable.VariableName, "=" ); for( int iIndex = 1; iIndex <= envVariable.EnvPaths.Count-1; iIndex++ ) { newLine = string.Concat(newLine, envVariable.EnvPaths[ iIndex-1 ] , ";" ); } if ( envVariable.EnvPaths.Count > 0 ) { newLine = string.Concat( newLine, envVariable.EnvPaths[ envVariable.EnvPaths.Count - 1 ] ); } newLine = string.Concat( newLine, Environment.NewLine ); stringBuilder.Append( newLine ); } return stringBuilder.ToString(); }
Just like before we can write the environment file back out to a new file.
string strenvFile = ConvertObjecttoEnvFileString(envFile); using (StreamWriter file = new StreamWriter(string.Concat(outputPath, "Env.txt"))) { file.Write(strenvFile); }