An adapter for converting Transactions into XElements, and vice-versa.
コード例 #1
0
        private void SaveTransactions(List<Transaction> transactions)
        {
            Uri accountUri = new Uri("/Accounts/Transactions/Transactions.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");
                }

                XElement resultingXElement = new XElement("Transactions");

                TransactionXElementAdapter adapter = new TransactionXElementAdapter();
                foreach (Transaction transaction in transactions) {
                    resultingXElement.Add(adapter.ToXElement(transaction));
                }

                using (StreamWriter sw = new StreamWriter(accountPart.GetStream())) {
                    sw.Write(resultingXElement.ToString());
                }
            }
        }
コード例 #2
0
        private List<Transaction> ReadTransactions(Workbook workbook, Account account)
        {
            List<Transaction> results = new List<Transaction>();
            Uri accountUri = new Uri("/Accounts/Transactions/Transactions.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 transactions = XElement.Load(reader);
                        TransactionXElementAdapter adapter = new TransactionXElementAdapter();
                        foreach (XElement transaction in transactions.Nodes()) {
                            Transaction fetchedTransaction = adapter.FromXElement(transaction, workbook);
                            if (account == null || fetchedTransaction.CreditAccount == account || fetchedTransaction.DebitAccount == account) {
                                results.Add(fetchedTransaction);
                            }
                        }
                    }
                }
            }
            return results;
        }