public static string ReloadCurrentUser(ref FileLocking _PassedFileStream, ref XDocument _PassedXdoc, string _PassedCurrentUser, ref Exception _PassedError) { _PassedFileStream = null; _PassedXdoc = null; // TODO: Check the empty string in "ReadFile" _PassedXdoc = XDocument.Load(_PassedFileStream.ReadFile("", ref _PassedError)); _PassedCurrentUser = _PassedXdoc.Element("ConfigFile").Element("ApplicationSettings").Element("GeneralSettings").Element("CurrentUser").Value.ToString(); return(_PassedCurrentUser); }
public static void SaveChanges(DataTable _PassedDatatableSheetOne, FileLocking _ConfigurationFile, XDocument _PassedXMLStream, int _passedColumnCount) { foreach (DataRow row in _PassedDatatableSheetOne.Rows) { List <string> xmlFileArray = new List <string>(); foreach (XElement x in _PassedXMLStream.Element("ConfigFile").Element("CustomerData").Elements("Customer")) { string commentRow = x.Element("Comment").Value ?? ""; xmlFileArray.Add(x.FirstAttribute.Value); xmlFileArray.Add(commentRow); } //string toDisplay = string.Join(Environment.NewLine, xmlFileArray); //MessageBox.Show(toDisplay); // Checks the array for customer ids, if they're in overwrite all elements within every customer id if (xmlFileArray.Contains(row[2].ToString())) { try { string commentRow = (row[_passedColumnCount].ToString() == null) ? "" : row[_passedColumnCount].ToString(); string inventoryRow = (row[_passedColumnCount + 1].ToString() == null) ? "" : row[_passedColumnCount + 1].ToString(); string dateRow = (row[_passedColumnCount + 2].ToString() == null) ? "" : row[_passedColumnCount + 2].ToString(); XElement xmlElements = _PassedXMLStream.Element("ConfigFile").Element("CustomerData").Elements("Customer").Single(x => x.Attribute("ID").Value == row[2].ToString()); //MessageBox.Show(commentRow + Environment.NewLine + inventoryRow + dateRow); xmlElements.Element("Comment").Value = commentRow; xmlElements.Element("Last_stocktaking").Value = inventoryRow; xmlElements.Element("Stocktaking_info").Value = dateRow; } catch (Exception _error) { // TODO: Localisize it CustomMessageBox.Show("Failure: " + _error.Message.ToString()); //TODO: Protocol it } } else // If they're not in the XML file, create them { string customerID = (row[2].ToString() == null) ? "" : row[2].ToString(); string customerName = (row[0].ToString() == null) ? "" : row[0].ToString(); string commentRow = (row[_passedColumnCount].ToString() == null) ? "" : row[_passedColumnCount].ToString(); string inventoryRow = (row[_passedColumnCount + 1].ToString() == null) ? "" : row[_passedColumnCount + 1].ToString(); string dateRow = (row[_passedColumnCount + 2].ToString() == null) ? "" : row[_passedColumnCount + 2].ToString(); _PassedXMLStream.Element("ConfigFile").Element("CustomerData").Add(new XElement("Customer", new XAttribute("ID", customerID), new XAttribute("Name", customerName), new XElement("Comment", commentRow), new XElement("Last_stocktaking", inventoryRow), new XElement("Stocktaking_info", dateRow))); } } _ConfigurationFile.WriteXDocument(_PassedXMLStream); //_ConfigurationFile.SetLength(0); //_PassedXMLStream.Save(_ConfigurationFile); // TODO: Clear - isn't needed anymore, cause when the configuration file is locked it already exists and there is no need that I check that //// Save all EDI relevant customer data //using (XmlWriter writer = XmlWriter.Create(_ConfigurationFile)) //{ // writer.WriteStartDocument(); // writer.WriteComment("This file is generated by the program. It contains all necessary data for the EDI customers"); // writer.WriteStartElement("ConfigFile"); // writer.WriteStartElement("FileData"); // writer.WriteStartElement("File_content"); // writer.WriteElementString("CustomerFile", ""); // writer.WriteEndElement(); // writer.WriteEndElement(); // writer.WriteStartElement("CustomerData"); // foreach (DataRow row in _PassedDatatableSheetOne.Rows) // { // string idRow = (row[2].ToString() == null) ? "" : row[2].ToString(); // string commentRow = (row[7].ToString() == null) ? "" : row[7].ToString(); // string inventoryRow = (row[8].ToString() == null) ? "" : row[8].ToString(); // string dateRow = (row[9].ToString() == null) ? "" : row[9].ToString(); // writer.WriteStartElement("Customer"); // writer.WriteAttributeString("ID", idRow); // writer.WriteElementString("Comment", commentRow); // writer.WriteElementString("Last_stocktaking", inventoryRow); // writer.WriteElementString("Stocktaking_info", dateRow); // writer.WriteEndElement(); // } // writer.WriteEndElement(); // writer.WriteEndElement(); // writer.WriteEndDocument(); //} }