private void tsmiImport_Click(object sender, EventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); dlg.DefaultExt = "rcp"; dlg.Filter = "Рабочая рецептура (*.rcp)|*.rcp"; dlg.Multiselect = false; DialogResult res = dlg.ShowDialog(); if (res == DialogResult.OK && !dlg.FileName.Equals("")) { try { XmlDocument doc = new XmlDocument(); doc.Load(CommonFunctions.AbsolutePathFromAnyPath(Application.StartupPath, dlg.FileName)); XmlNode root = doc.DocumentElement; DataRecept rcp = DataRecept.LoadFromXml(root, DataBook.Book, ReceptVersion.Version0); if (rcp != null) { DataBook.Book.Components.Add(rcp); } } catch (System.Exception ex) { MessageBox.Show("Невозможно импортировать файл:\n" + ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); } } }
/// <summary> /// загрузка данных по сырью из xml файла /// </summary> /// <param name="root">узел со списком компонент</param> /// <param name="par">родительская структура</param> /// <returns></returns> public static BindingList <DataBase> LoadManyFromXml(XmlNode root, DataBase par, ReceptVersion curVer) { BindingList <DataBase> ret = new BindingList <DataBase>(); switch (curVer) { case ReceptVersion.Version0: throw new NotImplementedException("not implemented"); case ReceptVersion.Version1: foreach (XmlNode curNode in root.ChildNodes) { if (curNode.Name.Equals("recept")) { ret.Add(DataRecept.LoadFromXml(curNode, par, curVer)); } else // это лист с сырьем { DataRaw curRaw = new DataRaw(par); curRaw.BeginUpdate(); foreach (XmlNode curRawNode in curNode.ChildNodes) { switch (curRawNode.Name) { case "quantity": curRaw.Quantity = Convert.ToDecimal(curRawNode.InnerText, CultureInfo.CurrentCulture); break; case "comment": curRaw.Comment = curRawNode.InnerText; break; case "brutto": curRaw.myBrutto = Convert.ToDecimal(curRawNode.InnerText, CultureInfo.CurrentCulture); break; case "rawStruct": curRaw.myRawStruct = DataRawStruct.LoadFromXml(curRawNode, curRaw, DataBaseType.RawType); break; case "processLoss": curRaw.myProcessLoss = DataRawStruct.LoadFromXml(curRawNode, curRaw, DataBaseType.ProcessLossType); break; } } curRaw.EndUpdate(false); curRaw.IsChanged = false; ret.Add(curRaw); } } break; default: throw new NotImplementedException("not implemented"); } return(ret); }
/// <summary> /// загрузка книги из файла /// </summary> private void LoadFromFile(string file, bool isVerbose) { this.BeginUpdate(); XmlDocument doc = new XmlDocument(); try { doc.Load(CommonFunctions.AbsolutePathFromAnyPath(Application.StartupPath, file)); XmlNode root = doc.DocumentElement; if (root.Attributes.Count > 0) { int curVer = int.Parse(root.Attributes["version"].Value, CultureInfo.CurrentCulture); if (curVer == 0) { myVersion = ReceptVersion.Version0; } if (curVer == 1) { myVersion = ReceptVersion.Version1; } } else { myVersion = ReceptVersion.CurrentVersion; // нет версии (rcp) } foreach (XmlNode rootProps in root.ChildNodes) { switch (rootProps.Name) { case "chief": m_chiefName = rootProps.InnerText; break; case "chiefPost": m_chiefPost = rootProps.InnerText; break; case "company": _company = rootProps.InnerText; break; case "developer": _developer = rootProps.InnerText; break; case "developerCompany": _developerCompany = rootProps.InnerText; break; case "recepts": foreach (XmlNode curNode in rootProps.ChildNodes) { Components.Add(DataRecept.LoadFromXml(curNode, this, myVersion)); } break; case "name": Name = rootProps.InnerText; break; } } } catch (XmlException ex) { if (isVerbose) { MessageBox.Show("Ошибка: " + ex.Message + "\n" + ex.StackTrace, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); } } catch (FileNotFoundException ex) { if (isVerbose) { MessageBox.Show("Ошибка: " + ex.Message + "\n" + ex.StackTrace, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); } } // сортировка элементов Components.Sort(new DataBaseComparer()); //Array.Sort(Components, new IfDataBaseNumSort()); RenumberRecepts(); EndUpdate(false); this.Changed += new EventHandler <DataBaseEventArgs>(DataBook_Changed); this.IsChanged = false; }