/// <summary> /// Parse input to xml /// </summary> /// <returns>Object {string Result } </returns> public static Output ConvertToXML(Parameters parameters, [PropertyTab] CsvInputParameters csvInputParameters, [PropertyTab] JsonInputParameters jsonInputParameters) { if (parameters.Input.GetType() != typeof(string)) { throw new InvalidDataException("The input data string was not in correct format. Supported formats are JSON, CSV and fixed length."); } if (parameters.Input.StartsWith("{") || parameters.Input.StartsWith("[")) { if (string.IsNullOrEmpty(jsonInputParameters.XMLRootElementName)) { throw new MissingFieldException("Root element name missing. Required with JSON input"); } if (jsonInputParameters.AppendToFieldName == null) { return new Output { Result = JsonConvert.DeserializeXmlNode(parameters.Input, jsonInputParameters.XMLRootElementName).OuterXml } } ; var jsonObject = (JObject)JsonConvert.DeserializeObject(parameters.Input); var newObject = ChangeNumericKeys(jsonObject, jsonInputParameters.AppendToFieldName); return(new Output { Result = JsonConvert.DeserializeXmlNode(JsonConvert.SerializeObject(newObject), jsonInputParameters.XMLRootElementName).OuterXml }); } if (!string.IsNullOrEmpty(csvInputParameters.CSVSeparator) && parameters.Input.Contains(csvInputParameters.CSVSeparator)) { using (var parser = new GenericParserAdapter()) { char?separator = Convert.ToChar(csvInputParameters.CSVSeparator); parser.SetDataSource(new StringReader(parameters.Input)); parser.ColumnDelimiter = separator; parser.FirstRowHasHeader = csvInputParameters.InputHasHeaderRow; parser.MaxBufferSize = 4096; parser.TrimResults = csvInputParameters.TrimOuputColumns; return(new Output { Result = parser.GetXml().OuterXml }); } } if (csvInputParameters.ColumnLengths == null) { throw new InvalidDataException("The input was recognized as fixed length file, but no column lengths were supplied."); } using (var parser = new GenericParserAdapter()) { var headerList = new List <int>(); foreach (var column in csvInputParameters.ColumnLengths) { headerList.Add(column.Length); } var headerArray = headerList.ToArray(); parser.SetDataSource(new StringReader(parameters.Input)); parser.ColumnWidths = headerArray; parser.FirstRowHasHeader = csvInputParameters.InputHasHeaderRow; parser.MaxBufferSize = 4096; parser.TrimResults = csvInputParameters.TrimOuputColumns; return(new Output { Result = parser.GetXml().OuterXml }); } }