/// <summary> /// Exports a single data-class. /// </summary> /// <param name="cmdLine"></param> private void ExportOneClass(ExportCommandLine cmdLine) { IXmlDataImex imex = ImexUtils.FindImexForDataClass(cmdLine.DataClass); int itemsPerFile = cmdLine.ItemsPerFile > 0 ? cmdLine.ItemsPerFile : ImexUtils.GetDefaultItemsPerFile(imex.GetType()); string directory; string baseFileName; // if the path has no extension, assume it specifies a directory if (string.IsNullOrEmpty(Path.GetExtension(cmdLine.Path))) { // use the name of the dataclass as a base filename directory = cmdLine.Path; baseFileName = cmdLine.DataClass; } else { // use the specified file name as the base filename directory = Path.GetDirectoryName(cmdLine.Path); baseFileName = Path.GetFileNameWithoutExtension(cmdLine.Path); } ImexUtils.Export(imex, directory, baseFileName, itemsPerFile); }
/// <summary> /// Imports data to the specified imex, from the specified path, which may be either a single file /// or a directory containing a set of .xml files. /// </summary> /// <param name="imex"></param> /// <param name="path"></param> public static void Import(IXmlDataImex imex, string path) { Platform.CheckForNullReference(path, "path"); // determine list of source files to import List <string> fileList = new List <string>(); if (File.Exists(path)) { fileList.Add(path); } else if (Directory.Exists(path)) { fileList.AddRange(Directory.GetFiles(path, "*.xml")); } else { throw new ArgumentException(string.Format("{0} is not a valid source file or directory.", path)); } imex.Import(ReadSourceFiles(fileList)); }
/// <summary> /// Executes the action specified by the command line arguments. /// </summary> /// <param name="cmdLine"></param> protected override void Execute(ImportCommandLine cmdLine) { IXmlDataImex imex = ImexUtils.FindImexForDataClass(cmdLine.DataClass); ImexUtils.Import(imex, cmdLine.Path); }
/// <summary> /// Imports data to the specified imex, from the specified path, which may be either a single file /// or a directory containing a set of .xml files. /// </summary> /// <param name="imex"></param> /// <param name="path"></param> public static void Import(IXmlDataImex imex, string path) { Platform.CheckForNullReference(path, "path"); // determine list of source files to import List<string> fileList = new List<string>(); if(File.Exists(path)) { fileList.Add(path); } else if(Directory.Exists(path)) { fileList.AddRange(Directory.GetFiles(path, "*.xml")); } else throw new ArgumentException(string.Format("{0} is not a valid source file or directory.", path)); imex.Import(ReadSourceFiles(fileList)); }
/// <summary> /// Exports data from the specified imex, to the specified directory using the specified base filename. /// </summary> /// <param name="imex"></param> /// <param name="directory"></param> /// <param name="baseFileName"></param> /// <param name="itemsPerFile"></param> public static void Export(IXmlDataImex imex, string directory, string baseFileName, int itemsPerFile) { if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) Directory.CreateDirectory(directory); // set a flag indicating whether all items should be exported to a single file bool oneFile = (itemsPerFile == 0); // if one file, set itemsPerFile to max value (which is effectively the same thing) if(oneFile) itemsPerFile = int.MaxValue; int itemCount = 0; int fileCount = 0; StreamWriter sw = null; XmlTextWriter writer = null; foreach (IExportItem item in imex.Export()) { if(itemCount % itemsPerFile == 0) { // close current file if(writer != null) { writer.WriteEndElement(); writer.Close(); sw.Close(); } // start new file string file = oneFile ? Path.Combine(directory, baseFileName) : Path.Combine(directory, baseFileName + (++fileCount)); if (!file.EndsWith(".xml")) file += ".xml"; // delete if already exists File.Delete(file); sw = new StreamWriter(File.OpenWrite(file)); writer = new XmlTextWriter(sw); writer.Formatting = System.Xml.Formatting.Indented; writer.WriteStartElement(RootTag); } item.Write(writer); itemCount++; } if (writer != null) { writer.WriteEndElement(); writer.Close(); sw.Close(); } }
/// <summary> /// Exports data from the specified imex, to the specified directory using the specified base filename. /// </summary> /// <param name="imex"></param> /// <param name="directory"></param> /// <param name="baseFileName"></param> /// <param name="itemsPerFile"></param> public static void Export(IXmlDataImex imex, string directory, string baseFileName, int itemsPerFile) { if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) { Directory.CreateDirectory(directory); } // set a flag indicating whether all items should be exported to a single file bool oneFile = (itemsPerFile == 0); // if one file, set itemsPerFile to max value (which is effectively the same thing) if (oneFile) { itemsPerFile = int.MaxValue; } int itemCount = 0; int fileCount = 0; StreamWriter sw = null; XmlTextWriter writer = null; foreach (IExportItem item in imex.Export()) { if (itemCount % itemsPerFile == 0) { // close current file if (writer != null) { writer.WriteEndElement(); writer.Close(); sw.Close(); } // start new file string file = oneFile ? Path.Combine(directory, baseFileName) : Path.Combine(directory, baseFileName + (++fileCount)); if (!file.EndsWith(".xml")) { file += ".xml"; } // delete if already exists File.Delete(file); sw = new StreamWriter(File.OpenWrite(file)); writer = new XmlTextWriter(sw); writer.Formatting = System.Xml.Formatting.Indented; writer.WriteStartElement(RootTag); } item.Write(writer); itemCount++; } if (writer != null) { writer.WriteEndElement(); writer.Close(); sw.Close(); } }