XiLibXml est une classe de gestion de fichiers XML par XPath. Elle permet d'ajouter, supprimer et d'éditer très facilement noeuds et attributs.
/// <summary> /// Crée le fichier xml de test si absent /// </summary> private void FichierXmlPresent() { if (System.IO.File.Exists("test.xml") == false) { int lnIndexNoeud; CXml lXml = new CXml(CXml.AffichageErreurs.Exeptions, CXml.UtilisationCData.Oui); lXml.NewXmlFile("test.xml", "TEST"); lXml.Noeud_Creer("/TEST", "MAILS"); string XPath = "/TEST/MAILS"; for (int i = 0; i < 4; i++) { lnIndexNoeud = lXml.Noeud_Creer(XPath, "MAIL", "test" + i + "@test.com"); lXml.AttributNoeud_Ajouter(XPath, "MAIL", "id", Guid.NewGuid().ToString(), lnIndexNoeud); lXml.AttributNoeud_Ajouter(XPath, "MAIL", "valide", true.ToString(), lnIndexNoeud); } lXml.SaveAndClose(); if (System.IO.File.Exists(Application.StartupPath + " est.xml")) webBrowser1.Url = new Uri(Application.StartupPath + " est.xml"); } } /// <summary> /// Charger les infos du fichier xml /// </summary> private void ChargerFichierXml() { CXml lXml = new CXml(CXml.AffichageErreurs.Exeptions, CXml.UtilisationCData.Oui); lXml.Load("test.xml"); lstContact.SelectedIndex = -1; lstContact.Items.Clear(); string XPath = "/TEST/MAILS/MAIL"; int lnNbrMail = lXml.Noeud_Nombre(XPath); for (int i = 0; i < lnNbrMail; i++) { string XPathMail = lXml.XPathFor(XPath, i); CMiniContact lMiniContact = new CMiniContact(); lMiniContact.Valide = CXml.ToBool(lXml.AttributNoeud_Valeur(XPathMail, "valide")); lMiniContact.Id = CXml.ToGuid(lXml.AttributNoeud_Valeur(XPathMail, "id")); lMiniContact.Mail = lXml.Noeud_Valeur(XPathMail); lstContact.Items.Add(lMiniContact); } lXml.Close(); if (System.IO.File.Exists(Application.StartupPath + " est.xml")) webBrowser1.Url = new Uri(Application.StartupPath + " est.xml"); } /// <summary> /// Enregistrer les modifs dans le fichier xml /// </summary> /// <param name="aMiniContactModifie"></param> /// <param name="abNouveau"></param> public void EnregistrerModifFichierXml(CMiniContact aMiniContactModifie, bool abNouveau) { CXml lXml = new CXml(CXml.AffichageErreurs.Exeptions, CXml.UtilisationCData.Oui); lXml.Load("test.xml"); string XPath; if (abNouveau == false) { XPath = String.Format("/TEST/MAILS/MAIL[@id=""]", aMiniContactModifie.Id); lXml.Noeud_AjouterValeur(XPath, aMiniContactModifie.Mail); lXml.AttributNoeud_Ajouter(XPath, "valide", aMiniContactModifie.Valide.ToString()); } else { XPath = "/TEST/MAILS"; int lnIndexNoeudCree = lXml.Noeud_Creer(XPath, "MAIL"); lXml.Noeud_AjouterValeur(XPath, "MAIL", aMiniContactModifie.Mail, lnIndexNoeudCree); lXml.AttributNoeud_Ajouter(XPath, "MAIL", "id", aMiniContactModifie.Id.ToString(), lnIndexNoeudCree); lXml.AttributNoeud_Ajouter(XPath, "MAIL", "valide", aMiniContactModifie.Valide.ToString(), lnIndexNoeudCree); } lXml.SaveAndClose(); } /// <summary> /// Supprimer un enregistrement dans le fichier xml /// </summary> public void SupprimerEnregistrementFichierXml(CMiniContact aMiniContactModifie) { CXml lXml = new CXml(CXml.AffichageErreurs.Exeptions, CXml.UtilisationCData.Oui); lXml.Load("test.xml"); lXml.Noeud_Supprimer("/TEST/MAILS", String.Format("MAIL[@id=""]", aMiniContactModifie.Id)); lXml.SaveAndClose(); }
Aucun commentaire
Ajouter un commentaire