// 0 = the file does not exist yet and so needs to be created // 1 = file exist and we just need to update it public static void SaveToFile(int saveType) { /* * System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); * System.Xml.XmlElement Nick,Pass,Profile,CreatedBy,DateCreated; * string xmlData = "<Accounts Version=\"1.0\"> </Accounts>"; * xmldoc.Load(new System.IO.StringReader(xmlData)); */ System.Xml.XmlTextWriter writer; writer = new System.Xml.XmlTextWriter(@"Accounts.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("Accounts"); /* * writer.WriteStartElement("Account"); * writer.WriteElementString("Nick","n"); * writer.WriteElementString("Pass","pg"); * writer.WriteElementString("ProfileName","pn"); * writer.WriteElementString("CreatedBy","c"); * writer.WriteElementString("DateCreated","d"); * writer.WriteEndElement(); */ writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); return; } writer.WriteStartElement("Accounts"); for (int iCount = 0; iCount < accounts.Count; iCount++) { account acc = (account)accounts[iCount]; writer.WriteStartElement("Account"); writer.WriteElementString("Nick", acc.nick); writer.WriteElementString("Pass", acc.pass); writer.WriteElementString("ProfileName", acc.Profile.profileName); writer.WriteElementString("CreatedBy", acc.createdBy); writer.WriteElementString("DateCreated", acc.DateCreated); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); /* * // The following code will take the all the accounts that are in * // settings and write them to an xml file called Accounts. * // NOTE. at the moment, it completly rewrite the old xml file. * // this seems a bit inifishent. * System.Xml.XmlElement[] Account = new System.Xml.XmlElement[accounts.Count]; * * for (int iCount = 0; iCount < accounts.Count; iCount++) * { * Account[iCount] = xmldoc.CreateElement("","Account",""); * * account acc = (account)accounts[iCount]; * Nick = xmldoc.CreateElement("","Nick",""); * Nick.InnerText = acc.nick; * * Pass = xmldoc.CreateElement("","Pass",""); * Pass.InnerText = acc.pass; * * Profile = xmldoc.CreateElement("","ProfileName",""); * Profile.InnerText = acc.Profile.profileName; * * CreatedBy = xmldoc.CreateElement("","CreatedBy",""); * CreatedBy.InnerText = acc.createdBy; * * DateCreated = xmldoc.CreateElement("","DateCreated",""); * DateCreated.InnerText = acc.DateCreated; * * Account[iCount].AppendChild(Nick); * Account[iCount].AppendChild(Pass); * Account[iCount].AppendChild(Profile); * Account[iCount].AppendChild(CreatedBy); * Account[iCount].AppendChild(DateCreated); * * xmldoc.DocumentElement.AppendChild(Account[iCount]); * } * * xmldoc.Save(@"Accounts.xml"); * // add code for saving the accounts. * * // Also add code incase settings value that is passed is null. * // if null is passed it means the file does not exist and we * // want to create one but with no current accounts in it. */ }
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); } }