public void SaveGroupMap(GroupMap groupMap) { GroupFileName = "groups.xml"; //hardcodé, pas un requis de le spécifier // On vérifie si le fichier existe, sinon on le crée XDocument xmlDoc; if (File.Exists(GroupFileName)) xmlDoc = XDocument.Load(GroupFileName); else xmlDoc = new XDocument(); if (xmlDoc.Descendants("Groups").Elements("Group").Count() == 0) //Doesn't work?? { //Group section doesn't exist XElement newGroup = new XElement("Groups", new XElement("Group", new XElement("GroupName", groupMap.GroupName), new XElement("Population", groupMap.Population), new XElement("User", groupMap.User) ) ); xmlDoc.Add(newGroup); xmlDoc.Save(GroupFileName); } else //Group section exists { XElement groups = xmlDoc.Element("Groups"); XElement newGroup = new XElement("Group", new XElement("GroupName", groupMap.GroupName), new XElement("Population", groupMap.Population), new XElement("User", groupMap.User) ); groups.Add(newGroup); xmlDoc.Save(GroupFileName); } }
static void Main(string[] args) { Console.WriteLine("Welcome to the Starcrafto manager! Input your command:"); bool wantToQuit = false; IXmlManager xmlHandle = new XmlHandler(); //On attend toujours des options while (!wantToQuit) { string [] fullCommand = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); //should capitalize [0] if (fullCommand.Length != 0) { switch (fullCommand[0]) { case "ADD": //Ajout d'un élément vers le XML if (fullCommand.Length == 5) //Si on a 5 parametres, on ajoute un user { User newUser = new User{ Name = fullCommand[1], Race = fullCommand[2], Pouvoir = fullCommand[3], Itude = fullCommand[4] }; xmlHandle.SaveUser(newUser); Console.WriteLine("Command executed: " + fullCommand[0] + " " + fullCommand[1] + " " + fullCommand[2] + " " + fullCommand[3] + " " + fullCommand[4]); } else if (fullCommand.Length == 4) //Si on a 4 parametres, on ajoute un groupe { GroupMap newGroup = new GroupMap { GroupName = fullCommand[2], Population = fullCommand[1], User = fullCommand[3] }; xmlHandle.SaveGroupMap(newGroup); Console.WriteLine("Command executed: " + fullCommand[0] + " " + fullCommand[1] + " " + fullCommand[2] + " " + fullCommand[3] + " "); } else // Sinon, on a un nb incorrect de param pour ADD { Console.WriteLine("Parametres incorrects!!"); break; } break; case "PRINT": //Afficher des infos à l'écran if (fullCommand.Length == 2) { if (fullCommand[1] == "Users") { List<User> allUsers = xmlHandle.ReadUserList(); foreach(User user in allUsers) { Console.WriteLine(user.Name + " " + user.Race + " " + user.Pouvoir + " " + user.Itude + " "); } } else if (fullCommand[1] == "POPULATION") { } } break; case "EXIT": wantToQuit = true; return; default: Console.WriteLine("Hmm, something happened. Wrong comand"); break; } } } Console.WriteLine("Press any key to exit."); Console.ReadLine(); }