/// <summary> /// Read all objects in a file /// </summary> /// <param name="fileName">The text file. May contain only one or multiple objects</param> /// <returns>List of ApplicationObjects</returns> public static List <ApplicationObject> ReadNavObjects(string fileName) { TxtFileModelInfo modelInfo = new TxtFileModelInfo(); TxtImporter importer = new TxtImporter(modelInfo); try { using (var instream = new FileStream(fileName, FileMode.Open)) { List <ApplicationObject> objects = importer.ImportFromStream(instream); if (objects != null && objects.Count > 0) { return(objects); } else { Console.WriteLine(@"Object could not be read from file {0}", fileName); } } } catch (Microsoft.Dynamics.Nav.Model.IO.Txt.TxtImportException e) { Console.WriteLine(@"Exception while reading {0}: {1}", fileName, e.Message); Console.WriteLine(@"Source line {0}, col {1}: {2}", e.LineNo, e.LinePos, e.Line); } catch (System.IO.IOException e) { Console.WriteLine(@"Exception while reading {0}: {1}", fileName, e.Message); } return(new List <ApplicationObject>()); }
/// <summary> /// Save all objects in the list to a file /// </summary> /// <param name="fileName">The text file where objects are written to</param> /// <param name="objects">A List of ApplicationObjects</param> public static void SaveNavObjects(string fileName, List <ApplicationObject> objects) { TxtFileModelInfo modelInfo = new TxtFileModelInfo(); TxtExporter export = new TxtExporter(modelInfo); try { using (var outstream = new FileStream(fileName, FileMode.Create)) { foreach (var obj in objects) { export.ExportObject(obj, outstream); } } } catch (Exception e) { Console.WriteLine(@"Exception while writing {0}: {1}", fileName, e.Message); } }