/// <summary> /// Creates the file "Experts.xml" and inserts the first Expert to it. /// </summary> /// <param name="expert">The object holding the data.</param> /// <returns></returns> public static bool firstAdd(Manager.ExpertManager.Expert expert) { try { XmlWriterSettings wSettings = new XmlWriterSettings(); wSettings.Indent = true; MemoryStream ms = new MemoryStream(); XmlWriter xw = XmlWriter.Create(ms, wSettings);// Write Declaration xw.WriteStartDocument(); // Write the root node xw.WriteStartElement("Experts"); // Write the expert and the expert elements xw.WriteStartElement("Expert"); xw.WriteStartAttribute("id"); xw.WriteString("0"); xw.WriteEndAttribute(); xw.WriteStartAttribute("login"); xw.WriteString(expert.getLogin()); xw.WriteEndAttribute(); xw.WriteStartAttribute("password"); xw.WriteString(expert.getPassword()); xw.WriteEndAttribute(); //---------------- xw.WriteStartElement("firstName"); xw.WriteString(expert.getFirstName()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("lastName"); xw.WriteString(expert.getLastName()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("email"); xw.WriteString(expert.getEmail()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("phoneNumber"); xw.WriteString(expert.getPhoneNumber()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("address"); xw.WriteString(expert.getAddress()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("role"); xw.WriteString(expert.getRole()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("specialty"); xw.WriteString(expert.getSpecialty()); xw.WriteEndElement(); //----------------- xw.WriteStartElement("status"); xw.WriteString(expert.getStatus().ToString()); xw.WriteEndElement(); //----------------- xw.WriteEndElement(); xw.WriteStartElement("MaxID"); xw.WriteString("0"); xw.WriteEndElement(); // Close the document xw.WriteEndDocument(); // Flush the write xw.Flush(); Byte[] buffer = new Byte[ms.Length]; buffer = ms.ToArray(); string xmlOutput = System.Text.Encoding.UTF8.GetString(buffer); //File.WriteAllText(path+"Experts.xml", xmlOutput); Helper.service.CreateXmlFile("Experts.xml", xmlOutput); } catch (Exception x) { return(false); } return(true); }
/// <summary> /// Modifies the Expert Data. /// </summary> /// <param name="login">The login of the target Expert object.</param> /// <param name="admin">The object that holds the new data.</param> /// <returns></returns> public static bool Modify(string login, Manager.ExpertManager.Expert expert) { //System.Windows.Forms.MessageBox.Show(expert.getFirstName(),"XMLExpert"); try { /* On utilise un XmlDocument et non un XPathDocument car ce dernier ne permet * pas l'édition des données XML. */ XmlDocument XmlDoc = new XmlDocument(); XPathNavigator Navigator; XPathNodeIterator Nodes; string rslt = Helper.service.LoadFile("Experts.xml").ToString(); StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\temp.xml"); sw.Write(rslt); sw.Close(); XmlDoc.Load(System.Windows.Forms.Application.StartupPath + "\\temp.xml"); Navigator = XmlDoc.CreateNavigator(); string ExpXPath = "//Expert[@login='******']"; Nodes = Navigator.Select(Navigator.Compile(ExpXPath)); if (Nodes.Count != 0) { /* Encodage des nouvelles données */ Nodes.MoveNext(); Nodes.Current.MoveToFirstAttribute(); Nodes.Current.MoveToNextAttribute(); Nodes.Current.SetValue(expert.getLogin()); Nodes.Current.MoveToNextAttribute(); Nodes.Current.SetValue(expert.getPassword()); Nodes.Current.MoveToParent(); Nodes.Current.MoveToFirstChild(); //System.Windows.Forms.MessageBox.Show(Nodes.Current.Name.ToString() + " | " + Nodes.Current.Value.ToString()); Nodes.Current.SetValue(expert.getFirstName()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getLastName()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getEmail()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getPhoneNumber()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getAddress()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getRole()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getSpecialty()); Nodes.Current.MoveToNext(XPathNodeType.Element); Nodes.Current.SetValue(expert.getStatus().ToString()); //XmlDoc.Save(path+"Experts.xml"); Helper.service.SaveXmlFile("Experts.xml", XmlDoc); } else { return(false); } } catch (Exception x) { return(false); } return(true); }
/// <summary> /// inserts an Expert to "Experts.xml". /// </summary> /// <param name="expert">The object holding the data</param> /// <returns></returns> public static bool insert(Manager.ExpertManager.Expert expert) { try { XmlDocument XmlDoc = new XmlDocument(); XPathNavigator Navigator; XPathNodeIterator Nodes; Int32 ID; /* Variable utilisée pour savoir quel est l'ID qu'il faut affecter au nouveau * noeud créé */ string rslt = Helper.service.LoadFile("Experts.xml").ToString(); StreamWriter sw = new StreamWriter(System.Windows.Forms.Application.StartupPath + "\\temp.xml"); sw.Write(rslt); sw.Close(); XmlDoc.Load(System.Windows.Forms.Application.StartupPath + "\\temp.xml"); Navigator = XmlDoc.CreateNavigator(); /* Recherche du noeud MaxID pour déterminer quelle sera l'ID du nouveau * expert. */ string ExpXPath = "//MaxID"; Nodes = Navigator.Select(Navigator.Compile(ExpXPath)); Nodes.MoveNext(); /* On place l'ID le plus élevé du document dans la variable ID */ ID = Nodes.Current.ValueAsInt; /* On incrémente la valeur du noeud MaxID car une fois notre nouveau noeud * créé, l'ID le plus élevé du document sera aussi incrémenté */ Nodes.Current.SetValue((ID + 1).ToString()); /* On se place sur le noeud ayant l'ID le plus élevé */ ExpXPath = "//Expert[@id='" + ID.ToString() + "']"; Nodes = Navigator.Select(Navigator.Compile(ExpXPath)); if (Nodes.Count != 0) { Nodes.MoveNext(); /* On crée le noeud principal (Client). */ Nodes.Current.InsertElementAfter("", "Expert", "", ""); /* On se place sur le noeud ainsi créé. */ Nodes.Current.MoveToNext(XPathNodeType.Element); ID++; /* On incrémente ID pour que sa valeur soit identique à celle se * trouvant dans le noeud MaxID. */ /* Encodage des données */ Nodes.Current.CreateAttribute("", "id", "", ID.ToString()); Nodes.Current.CreateAttribute("", "login", "", expert.getLogin()); Nodes.Current.CreateAttribute("", "password", "", expert.getPassword()); Nodes.Current.AppendChildElement("", "firstName", "", expert.getFirstName()); Nodes.Current.AppendChildElement("", "lastName", "", expert.getLastName()); Nodes.Current.AppendChildElement("", "email", "", expert.getEmail()); Nodes.Current.AppendChildElement("", "phoneNumber", "", expert.getPhoneNumber()); Nodes.Current.AppendChildElement("", "address", "", expert.getAddress()); Nodes.Current.AppendChildElement("", "role", "", expert.getRole()); Nodes.Current.AppendChildElement("", "specialty", "", expert.getSpecialty()); Nodes.Current.AppendChildElement("", "status", "", expert.getStatus().ToString()); //XmlDoc.Save(path+"Experts.xml"); Helper.service.SaveXmlFile("Experts.xml", XmlDoc); } else { return(false); } } catch (Exception x) { return(false); } return(true); }