/// <summary> /// override: loads the xml file and return the directory object array /// </summary> /// <param name="xmlFile"></param> /// <param name="loadMsg"></param> /// <returns></returns> public static XMLDirectoryObject[] LoadXMLFile(string xmlFile, ref string loadMsg) { XmlSerializer xmlSerial = null; FileStream fs = null; try { fs = new FileStream(xmlFile, FileMode.Open); XMLCopyScript xmlCopyScript = null; XMLDirectoryObject[] xmlDirList = null; //System.Type typ = xmlDirList.GetType(); xmlSerial = new XmlSerializer(typeof(XMLCopyScript)); xmlCopyScript = (XMLCopyScript)xmlSerial.Deserialize(fs); Common.TargetFolder = xmlCopyScript.copytopath; xmlDirList = (XMLDirectoryObject[])xmlCopyScript.directoryItems; fs.Close(); return(xmlDirList); } catch (Exception ex) { loadMsg = "Failed: " + ex.Message; if (fs != null) { fs.Close(); } return(null); } }
/// <summary> /// method to write the xml file to disk /// </summary> /// <param name="DirectoryTree"></param> /// <param name="msg"></param> /// <returns></returns> public static bool WriteXMLFile(ref System.Windows.Forms.TreeView DirectoryTree, ref string msg) { XmlSerializer xmlSerial = null; FileStream fs = null; try { if (File.Exists(_xmlfpne)) { File.Delete(_xmlfpne); } System.Threading.Thread.Sleep(10); fs = new FileStream(_xmlfpne, FileMode.Create); System.Windows.Forms.TreeNode dirnode; System.Windows.Forms.TreeNode filenode; XMLDirectoryObject[] xmlDirList = null; XMLFileObject[] xmlFileList = null; XMLCopyScript xmlCopyScript = null; if (DirectoryTree == null || DirectoryTree.Nodes.Count <= 0) { msg = "Empty directory tree."; return(false); } xmlCopyScript = new XMLCopyScript(Common.TargetFolder); xmlDirList = new XMLDirectoryObject[DirectoryTree.Nodes.Count]; //xmlCopyScript.directoryItems = xmlDirList; for (int i = 0; i < DirectoryTree.Nodes.Count; i++) { dirnode = DirectoryTree.Nodes[i]; //build the xml directory object xmlDirList[i] = new XMLDirectoryObject(); xmlDirList[i].fullpath = ((DirectoryInfo)dirnode.Tag).FullName; xmlDirList[i].ischecked = Convert.ToInt16(dirnode.Checked); xmlFileList = new XMLFileObject[dirnode.Nodes.Count]; //enumerate the file items for the directory for (int j = 0; j < dirnode.Nodes.Count; j++) { filenode = dirnode.Nodes[j]; xmlFileList[j] = new XMLFileObject(); xmlFileList[j].fullpath = ((FileInfo)filenode.Tag).FullName; xmlFileList[j].ischecked = Convert.ToInt16(dirnode.Checked); } xmlDirList[i].fileItems = xmlFileList; } xmlCopyScript.directoryItems = xmlDirList; System.Type typ = xmlCopyScript.GetType(); xmlSerial = new XmlSerializer(typ); xmlSerial.Serialize(fs, xmlCopyScript); return(true); } catch (Exception ex) { if (fs != null) { fs.Close(); } msg = ex.Message; return(false); } }