public void BuyProducts() { try { string patchFile = @"bank.xml"; if (!File.Exists(patchFile)) { XDocument xDoc = new XDocument ( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Bank", new XElement("Accounts", new XElement("Account", new XAttribute("title", "user"), new XElement("balance", 100)), new XElement("Account", new XAttribute("title", "admin"), new XElement("balance", 0) )))); xDoc.Save(patchFile); Console.WriteLine("The file does not exist, so a new file was created...\nTry click again..."); Console.ReadKey(); } else if (File.Exists(patchFile)) { decimal pathBalance = BuySell.Buy(); XmlDocument doc = new XmlDocument(); doc.Load(patchFile); money = GetBalance(); if (money == 0 || money < 0) { Console.WriteLine("You haven't balance"); } else { Console.WriteLine("Your balance reduced: -" + pathBalance); money -= pathBalance; doc.SelectSingleNode("Bank/Accounts/Account[@title='user']/balance").InnerText = Convert.ToString(money); Console.WriteLine("Your balance left: " + money); XDocument docX = XDocument.Load(patchFile); decimal result = 0; var node = from accounts in docX.Descendants("Account") where (string)accounts.Attribute("title") == "admin" select new { balance = accounts.Element("balance").Value }; foreach (var nod in node) { result = Convert.ToDecimal(nod.balance); } result += pathBalance; doc.SelectSingleNode("Bank/Accounts/Account[@title='admin']/balance").InnerText = Convert.ToString(result); doc.Save(patchFile); } Console.ReadKey(); } } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadKey(); } }
static void RemoveProducts() { BuySell.RemoveProducts(); }