/// <summary> /// Validates the given flat file. /// </summary> /// <param name="flatFileFilename">The name of the flat file to validate.</param> /// <param name="schemaTreeWithLoadedSchema">The schema tree with the schema loaded.</param> /// <param name="schemaClassName">The name of the schema class.</param> /// <returns>The flat file contents converted to XML.</returns> /// <exception cref="ApplicationException">When there is a validation error.</exception> public static string ValidateFlatFile(string flatFileFilename, CXSDSchemaTree schemaTreeWithLoadedSchema, string schemaClassName) { if (schemaTreeWithLoadedSchema == null) { throw new ArgumentNullException(nameof(schemaTreeWithLoadedSchema)); } ITOMErrorInfo[] validationErrors = null; string xmlOutput = null; if (!schemaTreeWithLoadedSchema.ValidateInstance(flatFileFilename, Microsoft.BizTalk.TOM.OutputInstanceType.Native, schemaClassName, out validationErrors, out xmlOutput)) { string message = "No details provided."; if (validationErrors != null) { var messages = validationErrors.Select(e => $"Line:{e.LineNumber} Position:{e.LinePosition} {(e.IsWarning ? "Warning: " : "Error: ")} {e.ErrorInfo}"); message = string.Join(". " + Environment.NewLine, messages); } throw new InvalidDataException($"An error occurred while parsing/validating the contents of the flat file, or converting it to XML: {Environment.NewLine}{message}"); } return(xmlOutput); }
/// <summary> /// Validates the given flat file contents. /// </summary> /// <param name="flatFileContents">The contents of the flat file to validate.</param> /// <param name="schemaTreeWithLoadedSchema">The schema tree with the schema loaded.</param> /// <param name="schemaClassName">The name of the schema class.</param> /// <returns>The flat file contents converted to XML.</returns> /// <exception cref="ApplicationException">When there is a validation error.</exception> public static string ValidateFlatFileContents(string flatFileContents, CXSDSchemaTree schemaTreeWithLoadedSchema, string schemaClassName) { var tempFlatFileFilename = Path.GetTempFileName(); try { File.WriteAllText(tempFlatFileFilename, flatFileContents); return(ValidateFlatFile(tempFlatFileFilename, schemaTreeWithLoadedSchema, schemaClassName)); } finally { if (File.Exists(tempFlatFileFilename)) { File.Delete(tempFlatFileFilename); } } }