//-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment specification. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateSpecificationString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_NTPServer))
                {
                    if (resultInfo.title != null)
                    {
                        // Write the server url
                        sw.WriteLine(swArgument, LabConsts.STR_Server_Url, resultInfo.serverUrl);
                    }
                    else
                    {
                        // Previous version - write the server name
                        sw.WriteLine(swArgument, LabConsts.STR_TimeServer, resultInfo.serverName);
                    }
                }

                // Write the format name
                sw.WriteLine(swArgument, LabConsts.STR_TimeFormat, resultInfo.formatName);
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment specification. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateSpecificationString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsSpeed) == true ||
                    resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_SpeedVsVoltage) == true)
                {
                    sw.WriteLine(swArgument, LabConsts.STR_MinSpeed, resultInfo.speedMin);
                    sw.WriteLine(swArgument, LabConsts.STR_MaxSpeed, resultInfo.speedMax);
                    sw.WriteLine(swArgument, LabConsts.STR_SpeedStep, resultInfo.speedStep);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsField) == true ||
                    resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_SpeedVsField) == true)
                {
                    sw.WriteLine(swArgument, LabConsts.STR_MinField, resultInfo.fieldMin);
                    sw.WriteLine(swArgument, LabConsts.STR_MaxField, resultInfo.fieldMax);
                    sw.WriteLine(swArgument, LabConsts.STR_FieldStep, resultInfo.fieldStep);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsLoad) == true)
                {
                    sw.WriteLine(swArgument, LabConsts.STR_MinLoad, resultInfo.loadMin);
                    sw.WriteLine(swArgument, LabConsts.STR_MaxLoad, resultInfo.loadMax);
                    sw.WriteLine(swArgument, LabConsts.STR_LoadStep, resultInfo.loadStep);
                }
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment result. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateResultsString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_RadioactivityVsAbsorber) == true)
                {
                    sw.WriteLine(swArgument, STR_CountsForAbsorber, string.Empty);

                    for (int i = 0; i < resultInfo.datavectors.GetLength(0); i++)
                    {
                        //
                        // Create a CSV string of radioactivity counts from the data vector
                        //
                        string csvCounts = string.Empty;
                        for (int j = 0; j < resultInfo.datavectors.GetLength(1); j++)
                        {
                            if (j > 0)
                            {
                                csvCounts += LabConsts.CHR_CsvSplitter.ToString();
                            }
                            csvCounts += resultInfo.datavectors[i, j].ToString();
                        }

                        // Write the string of counts showing the absorber
                        sw.WriteLine(swArgument, resultInfo.absorbers[i], csvCounts);
                    }
                }
                else
                {
                    sw.WriteLine(swArgument, STR_CountsAtDistance, string.Empty);

                    for (int i = 0; i < resultInfo.datavectors.GetLength(0); i++)
                    {
                        //
                        // Create a CSV string of radioactivity counts from the data vector
                        //
                        string csvCounts = string.Empty;
                        for (int j = 0; j < resultInfo.datavectors.GetLength(1); j++)
                        {
                            if (j > 0)
                            {
                                csvCounts += LabConsts.CHR_CsvSplitter.ToString();
                            }
                            csvCounts += resultInfo.datavectors[i, j].ToString();
                        }

                        // Write the string of counts showing the distance
                        sw.WriteLine(swArgument, resultInfo.distances[i].ToString() + STR_Millimetres, csvCounts);
                    }
                }
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment result. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateResultsString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_LockedRotor))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_PhasePhaseVoltage, resultInfo.voltage);
                    sw.WriteLine(swArgument, LabConsts.STR_PhaseCurrent, resultInfo.current);
                    sw.WriteLine(swArgument, LabConsts.STR_PowerFactor, resultInfo.powerFactor);
                    if (resultInfo.title != null)
                    {
                        sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speed);
                    }
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_NoLoad))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_PhasePhaseVoltage, resultInfo.voltage);
                    sw.WriteLine(swArgument, LabConsts.STR_PhaseCurrent, resultInfo.current);
                    sw.WriteLine(swArgument, LabConsts.STR_PowerFactor, resultInfo.powerFactor);
                    if (resultInfo.title != null)
                    {
                        sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speed);
                    }
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_SynchronousSpeed))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_PhasePhaseVoltage, resultInfo.voltage);
                    sw.WriteLine(swArgument, LabConsts.STR_PhaseCurrent, resultInfo.current);
                    sw.WriteLine(swArgument, LabConsts.STR_PowerFactor, resultInfo.powerFactor);
                    if (resultInfo.title != null)
                    {
                        sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speed);
                    }
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_FullLoad))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_PhasePhaseVoltage, resultInfo.voltage);
                    sw.WriteLine(swArgument, LabConsts.STR_PhaseCurrent, resultInfo.current);
                    sw.WriteLine(swArgument, LabConsts.STR_PowerFactor, resultInfo.powerFactor);
                    if (resultInfo.title != null)
                    {
                        sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speed);
                    }
                }
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment specification. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateSpecificationString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                //
                // YOUR CODE HERE
                //
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment result. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateResultsString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsSpeed))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speedVector);
                    sw.WriteLine(swArgument, LabConsts.STR_ArmatureVoltage, resultInfo.voltageVector);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsField))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_FieldCurrent, resultInfo.fieldVector);
                    sw.WriteLine(swArgument, LabConsts.STR_ArmatureVoltage, resultInfo.voltageVector);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_VoltageVsLoad))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_LoadTorque, resultInfo.loadVector);
                    sw.WriteLine(swArgument, LabConsts.STR_ArmatureVoltage, resultInfo.voltageVector);
                    sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speedVector);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_SpeedVsVoltage))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_ArmatureVoltage, resultInfo.voltageVector);
                    sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speedVector);
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_SpeedVsField))
                {
                    sw.WriteLine(swArgument, LabConsts.STR_FieldCurrent, resultInfo.fieldVector);
                    sw.WriteLine(swArgument, LabConsts.STR_MotorSpeed, resultInfo.speedVector);
                    sw.WriteLine(swArgument, LabConsts.STR_ArmatureVoltage, resultInfo.voltageVector);
                }
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment result. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateResultsString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.title != null)
                {
                    sw.WriteLine(swArgument, LabConsts.STR_TimeOfDay, resultInfo.timeofday);
                }
                sw.WriteLine(swArgument, LabConsts.STR_DayOfWeek, resultInfo.dayofweek);
                sw.WriteLine(swArgument, LabConsts.STR_Day, resultInfo.day);
                sw.WriteLine(swArgument, LabConsts.STR_Month, resultInfo.month);
                sw.WriteLine(swArgument, LabConsts.STR_Year, resultInfo.year);
                sw.WriteLine(swArgument, LabConsts.STR_Hours, resultInfo.hours);
                sw.WriteLine(swArgument, LabConsts.STR_Minutes, resultInfo.minutes);
                sw.WriteLine(swArgument, LabConsts.STR_Seconds, resultInfo.seconds);
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment result. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateResultsString(ResultInfo resultInfo, string swArgument)
        {
            ResultInfo.Measurement[] measurements = null;
            StringWriter sw = new StringWriter();
            try
            {
                if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_OpenCircuitVaryField))
                {
                    measurements = new ResultInfo.Measurement[] {
                        resultInfo.fieldCurrent,
                        resultInfo.speed,
                        resultInfo.voltage,
                    };
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_OpenCircuitVarySpeed))
                {
                    measurements = new ResultInfo.Measurement[] {
                        resultInfo.speed,
                        resultInfo.fieldCurrent,
                        resultInfo.voltage,
                    };
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_ShortCircuitVaryField))
                {
                    measurements = new ResultInfo.Measurement[] {
                        resultInfo.fieldCurrent,
                        resultInfo.speed,
                        resultInfo.statorCurrent,
                    };
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_PreSynchronisation))
                {
                    measurements = new ResultInfo.Measurement[] {
                        resultInfo.fieldCurrent,
                        resultInfo.speedSetpoint,
                        resultInfo.mainsVoltage,
                        resultInfo.mainsFrequency,
                        resultInfo.syncVoltage,
                        resultInfo.syncFrequency,
                        resultInfo.syncMainsPhase,
                        resultInfo.synchronism,
                    };
                }
                else if (resultInfo.setupId.Equals(LabConsts.STRXML_SetupId_Synchronisation))
                {
                    measurements = new ResultInfo.Measurement[] {
                        resultInfo.torqueSetpoint,
                        resultInfo.fieldCurrent,
                        resultInfo.syncVoltage,
                        resultInfo.syncFrequency,
                        resultInfo.powerFactor,
                        resultInfo.realPower,
                        resultInfo.reactivePower,
                        resultInfo.phaseCurrent,
                    };
                }

                for (int i = 0; i < measurements.Length; i++)
                {
                    ResultInfo.Measurement measurement = measurements[i];

                    //
                    // Check if the units have been specified
                    //
                    string title;
                    if (measurement.units == null || measurement.units.Trim().Length == 0)
                    {
                        // Units not specified
                        title = string.Format(STR_Name_fmt, measurement.name);
                    }
                    else
                    {
                        // Units specified
                        title = string.Format(STR_NameUnits_fmt, measurement.name, measurement.units);
                    }
                    sw.WriteLine(swArgument, title, this.ReformatValues(measurement.values));
                }
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        /// <summary>
        /// Create a string which represents the experiment specification. Each line contains two fields
        /// which are the name of the field and its value. The format of the string will be different
        /// for comma-seperated-values and applet parameters.
        /// </summary>
        /// <param name="xmlNodeExperimentResult"></param>
        /// <param name="swArgument"></param>
        /// <returns></returns>
        public string CreateSpecificationString(ResultInfo resultInfo, string swArgument)
        {
            StringWriter sw = new StringWriter();
            try
            {
                // Write the source name
                sw.WriteLine(swArgument, STR_Source, resultInfo.sourceName);

                //
                // Create a CSV string of absorbers from the absorber list
                //
                string csvAbsorbers = string.Empty;
                for (int i = 0; i < resultInfo.absorbers.Length; i++)
                {
                    if (i > 0)
                    {
                        csvAbsorbers += LabConsts.CHR_CsvSplitter.ToString();
                    }
                    csvAbsorbers += resultInfo.absorbers[i].ToString();
                }

                // Write the absorbers
                if (resultInfo.absorbers.Length > 1)
                {
                    // Write as an absorber list
                    sw.WriteLine(swArgument, STR_AbsorberList, csvAbsorbers);
                }
                else
                {
                    // Write as a single absorber
                    sw.WriteLine(swArgument, STR_Absorber, csvAbsorbers);
                }

                //
                // Create a CSV string of distances from the distance list
                //
                string csvDistances = string.Empty;
                for (int i = 0; i < resultInfo.distances.Length; i++)
                {
                    if (i > 0)
                    {
                        csvDistances += LabConsts.CHR_CsvSplitter.ToString();
                    }
                    csvDistances += resultInfo.distances[i].ToString();
                }

                // Write the distances
                if (resultInfo.distances.Length > 1)
                {
                    // Write as a distance list
                    sw.WriteLine(swArgument, STR_DistanceList, csvDistances);
                }
                else
                {
                    // Write as a single distance
                    sw.WriteLine(swArgument, STR_Distance, csvDistances);
                }

                // Write the duration
                sw.WriteLine(swArgument, STR_Duration, resultInfo.duration);

                // Write the repeat count (number of trials)
                sw.WriteLine(swArgument, STR_Trials, resultInfo.repeat);
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return sw.ToString();
        }
        //-------------------------------------------------------------------------------------------------//
        private string BuildTableResult(ResultInfo resultInfo, StatusCodes statusCode, string errorMessage)
        {
            string strTableResult = string.Empty;

            try
            {
                StringWriter sw = new StringWriter();
                sw.WriteLine(STR_SwTblBegin);

                //
                // Experiment information
                //
                sw.WriteLine(STR_SwTblBlankRow);
                sw.WriteLine(STR_SwTblHdrArgument, STR_ExperimentInformation);
                sw.WriteLine(STR_SwTblArgument, STR_Timestamp, resultInfo.timestamp);
                sw.WriteLine(STR_SwTblArgument, STR_ExperimentID, resultInfo.experimentId);
                if (resultInfo.title != null)
                {
                    sw.WriteLine(STR_SwTblArgument, STR_UnitID, resultInfo.unitId);
                    if (resultInfo.dataType != null)
                    {
                        sw.WriteLine(STR_SwTblArgument, STR_DataType, resultInfo.dataType);
                    }
                }

                //
                // Experiment setup
                //
                sw.WriteLine(STR_SwTblBlankRow);
                sw.WriteLine(STR_SwTblHdrArgument, STR_ExperimentSetup);
                if (resultInfo.setupName != null)
                {
                    sw.WriteLine(STR_SwTblArgument, STR_setupName, resultInfo.setupName);
                }
                string csvSpecification = labResults.CreateSpecificationString(resultInfo, STR_SwTblArgument);
                sw.Write(csvSpecification);

                //
                // Experiment results
                //
                sw.WriteLine(STR_SwTblBlankRow);
                sw.WriteLine(STR_SwTblHdrArgument, STR_ExperimentResults);

                //
                // Check if experiment had completed successfully
                //
                if (statusCode == StatusCodes.Completed)
                {
                    // Include experiment results
                    string tblResults = labResults.CreateResultsString(resultInfo, STR_SwTblArgument);
                    sw.Write(tblResults);
                }
                else
                {
                    sw.WriteLine(STR_SwTblArgument, STR_ErrorMessage, errorMessage);
                }

                sw.WriteLine(STR_SwTblEnd);

                strTableResult = sw.ToString();
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return strTableResult;
        }
        //-------------------------------------------------------------------------------------------------//
        private string BuildCsvResult(ResultInfo resultInfo)
        {
            string strCsvResult = string.Empty;

            try
            {
                StringWriter sw = new StringWriter();

                //
                // Experiment information
                //
                sw.WriteLine();
                sw.WriteLine(STR_CsvExperimentInformation);
                sw.WriteLine(STR_SwCsvArgument, STR_Timestamp, resultInfo.timestamp);
                sw.WriteLine(STR_SwCsvArgument, STR_ExperimentID, resultInfo.experimentId);
                if (resultInfo.title != null)
                {
                    sw.WriteLine(STR_SwCsvArgument, STR_UnitID, resultInfo.unitId);
                    if (resultInfo.dataType != null)
                    {
                        sw.WriteLine(STR_SwCsvArgument, STR_DataType, resultInfo.dataType);
                    }
                }

                //
                // Experiment setup
                //
                sw.WriteLine();
                sw.WriteLine(STR_CsvExperimentSetup);
                if (resultInfo.title != null)
                {
                    sw.WriteLine(STR_SwCsvArgument, STR_setupName, resultInfo.setupName);
                }
                string csvSpecification = labResults.CreateSpecificationString(resultInfo, STR_SwCsvArgument);
                sw.Write(csvSpecification);

                //
                // Experiment results
                //
                sw.WriteLine();
                sw.WriteLine(STR_CsvExperimentResults);

                string csvResults = labResults.CreateResultsString(resultInfo, STR_SwCsvArgument);
                sw.Write(csvResults);

                strCsvResult = sw.ToString();
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return strCsvResult;
        }
        //-------------------------------------------------------------------------------------------------//
        private string BuildAppletParams(ResultInfo resultInfo)
        {
            string strAppletParams = string.Empty;

            try
            {
                StringWriter sw = new StringWriter();

                // Experiment specification
                sw.Write(labResults.CreateSpecificationString(resultInfo, STR_SwAppletArgument));

                // Experiment results
                sw.Write(labResults.CreateResultsString(resultInfo, STR_SwAppletArgument));

                strAppletParams = sw.ToString();
            }
            catch (Exception ex)
            {
                Logfile.WriteError(ex.Message);
            }

            return strAppletParams;
        }