public static void SaveToFile(int saveType) { System.Xml.XmlTextWriter writer; writer = new System.Xml.XmlTextWriter(@"Profiles.xml", System.Text.Encoding.GetEncoding("windows-1252")); writer.Formatting = System.Xml.Formatting.Indented; writer.WriteStartDocument(); // this will normly happen when there is no xml file // and one needs creating if (saveType == 0) { writer.WriteStartElement("Profiles"); writer.WriteStartElement("Profile"); writer.WriteElementString("isOperator", "false"); writer.WriteElementString("OnJoinMessage", "Join message for normle users"); writer.WriteElementString("ProfileLevel", "0"); writer.WriteElementString("ProfileName", "DefaultProfile"); writer.WriteElementString("kickLevel", "0"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); writer = null; return; } writer.WriteStartElement("Profiles"); System.Xml.XmlElement[] Account = new System.Xml.XmlElement[Profiles.Count]; for (int iCount = 0; iCount < Profiles.Count; iCount++) { profile pro = (profile)Profiles[iCount]; pro.OnJoinMessage = pro.OnJoinMessage.Replace("'", "'"); pro.profileName = pro.profileName.Replace("'", "'"); writer.WriteStartElement("Profile"); writer.WriteElementString("isOperator", pro.isOP.ToString()); writer.WriteElementString("OnJoinMessage", pro.OnJoinMessage); writer.WriteElementString("ProfileLevel", pro.profileLevel.ToString()); writer.WriteElementString("ProfileName", pro.profileName); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); writer = null; }
public static void LoadFromFile() { System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); try { xmldoc.Load(@"Accounts.xml"); } catch (System.IO.FileNotFoundException) { SaveToFile(0); xmldoc.Load(@"Accounts.xml"); } // Find out how many accounts we are dealing with int NumberOfAccounts = xmldoc.DocumentElement.ChildNodes.Count; // if the file exists but there are not accounts present in it. exit the function if (NumberOfAccounts < 1) { return; } // will hold each account so they can be examined System.Xml.XmlNode EachAccount; // Go through each account in the xml file for (int iCount = 0; iCount < NumberOfAccounts; iCount++) { // hold the data for each Account in EachAccount EachAccount = xmldoc.DocumentElement.ChildNodes.Item(iCount); // create an instance for each user account, other wise when we try to // add data to userAccounts it will error account acc = new account(); // Go through each node with in each account. e.g. nick then pass etc. for (int iInnerNodeCount = 0; iInnerNodeCount < EachAccount.ChildNodes.Count; iInnerNodeCount++) { // find out which item we are currently dealing with and then store the info // into userSettings[] array. string temp = EachAccount.ChildNodes.Item(iInnerNodeCount).LocalName.ToLower(); switch (EachAccount.ChildNodes.Item(iInnerNodeCount).LocalName) { case "Nick": // The inner text contains the name of the person acc.nick = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; break; case "Pass": acc.pass = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; break; case "ProfileName": profile pro = profiles.GetProfile(EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText); if (pro == null) { acc.Profile = new profile(); } else { acc.Profile = pro; } break; case "CreatedBy": acc.createdBy = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; break; case "DateCreated": acc.DateCreated = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; break; } } accounts.Add(acc); } }
public static void LoadFromFile() { System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); try { xmldoc.Load(@"Profiles.xml"); } catch (System.IO.FileNotFoundException) { SaveToFile(0); xmldoc.Load(@"Profiles.xml"); } // Find out how many accounts we are dealing with int NumberOfAccounts = xmldoc.DocumentElement.ChildNodes.Count; // if the file exists but there are not accounts present in it. exit the function if (NumberOfAccounts < 1) { return; } // will hold each account so they can be examined System.Xml.XmlNode EachAccount; // Go through each account in the xml file for (int iCount = 0; iCount < NumberOfAccounts; iCount++) { // hold the data for each profile in EachAccount EachAccount = xmldoc.DocumentElement.ChildNodes.Item(iCount); profile pro = new profile(); // Go through each node with in each profile. e.g. nick then pass etc. for (int iInnerNodeCount = 0; iInnerNodeCount < EachAccount.ChildNodes.Count; iInnerNodeCount++) { // find out which item we are currently dealing with and then store the info // into userSettings[] array. string temp = EachAccount.ChildNodes.Item(iInnerNodeCount).LocalName.ToLower(); switch (EachAccount.ChildNodes.Item(iInnerNodeCount).LocalName) { case "isOperator": try { pro.isOP = bool.Parse(EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText); } catch (System.Exception) { pro.isOP = false; } break; case "OnJoinMessage": pro.OnJoinMessage = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; pro.OnJoinMessage = pro.OnJoinMessage.Replace("'", ","); break; case "ProfileLevel": try { pro.profileLevel = int.Parse(EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText); } catch (System.Exception) { pro.profileLevel = 0; } break; case "ProfileName": pro.profileName = EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText; pro.profileName = pro.profileName.Replace("'", ","); break; case "kickLevel": pro.kickLevel = int.Parse(EachAccount.ChildNodes.Item(iInnerNodeCount).InnerText); break; } } if (pro.profileName == "DefaultProfile") { DefaultPro = pro; } Profiles.Add(pro); } if (DefaultPro == null) { DefaultPro = new profile(); } }