private void WriteXmlFile(Outputmodels.POChinoOutput document, string path) { StringWriter sw = new StringWriter(); XmlTextWriter tw = null; try { tw = new XmlTextWriter(path, Encoding.UTF8); tw.Formatting = Formatting.Indented; tw.Indentation = 4; tw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\""); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer serializer = new XmlSerializer(document.GetType()); serializer.Serialize(tw, document, ns); } catch (Exception ex) { _logger.LogError("WriteXmlFile - Error while generating xml file", ex, ex.Message); } finally { sw.Close(); if (tw != null) { tw.Close(); } } }
public async Task <ApiResult> WriteChinoFile(List <POChinoOutput> chinoOrders) { //Log message if directory not found and return if (!Directory.Exists(Path.GetDirectoryName(_outputSettings.Value.OutputFilePath))) { _logger.LogError("WriteChinoFile - No Directory found with given path- POChino: {Reason}", _outputSettings.Value.OutputFilePath); return(new ApiResult <string>()); } //write data into xml output var chinoHeader = BuildChinoHeader(); var chinoMessage = BuildChinoMessage(chinoOrders); Outputmodels.POChinoOutput chinoFileOutput = new Outputmodels.POChinoOutput { Header = chinoHeader, Message = chinoMessage }; if (chinoMessage.Orders != null && chinoMessage.Orders.Count > 0) { string path = GetOutputFilepath(); if (string.IsNullOrEmpty(path)) { _logger.LogError("WriteChinoFile - Output path not specified."); return(new ApiResult <string>()); } WriteXmlFile(chinoFileOutput, path); //Make sure the file was actually written to disk before finalizing if (!File.Exists(path)) { _logger.LogError($"WriteChinoFile - Output failed to write successfully to {path}"); return(new ApiResult <string>()); } _logger.LogInformation($"Output successfully written to {path}."); } else { _logger.LogInformation($"No output written; no new or changed data found."); } return(new ApiResult <string>()); }