private void SaveAccount(Account account) { // Add the account ID to the index List<Guid> accountIDs = ReadIndex(account.AccountType); if (!accountIDs.Contains(account.AccountID)) { accountIDs.Add(account.AccountID); SaveIndex(accountIDs, account.AccountType); } AccountXElementAdapter adapter = new AccountXElementAdapter(); XElement xmlElement = adapter.ToXElement(account); Uri accountUri = new Uri("/Accounts/" + account.AccountID.ToString() + ".xml", UriKind.Relative); using (Package package = ZipPackage.Open(this.FilePath, System.IO.FileMode.OpenOrCreate)) { // Either get the existing PackagePart or create a new one PackagePart accountPart = null; if (package.PartExists(accountUri)) { accountPart = package.GetPart(accountUri); } else { accountPart = package.CreatePart(accountUri, "text/xml"); } // Now write the account, overwriting whatever was there previously // Note: If concurrency checks were to be implemented (at this stage, they wouldn't // make much sense as TrialBalance is a single-user application) they should be // implemented here. using (StreamWriter sw = new StreamWriter(accountPart.GetStream())) { sw.Write(xmlElement.ToString()); } } }
private Account ReadAccount(Guid accountId) { Account result = null; Uri accountUri = new Uri("/Accounts/" + accountId.ToString() + ".xml", UriKind.Relative); using (Package package = ZipPackage.Open(this.FilePath, System.IO.FileMode.OpenOrCreate)) { if (package.PartExists(accountUri)) { PackagePart accountPart = package.GetPart(accountUri); using (StreamReader reader = new StreamReader(accountPart.GetStream())) { XElement xml = XElement.Load(reader); AccountXElementAdapter adapter = new AccountXElementAdapter(); result = adapter.FromXElement(xml); } } } return result; }