Example #1
0
        /// <summary>
        /// Returns list of all transactions in OFX document
        /// </summary>
        /// <param name="ofxDocument">OFX Document</param>
        /// <param name="xmlDocument">XML Document</param>
        /// <returns>List of transactions found in OFX document</returns>
        private void ImportTransations(OfxDocument ofxDocument, XmlNode xmlDocument)
        {
            var xpath = GetXPath(ofxDocument.AccType, OfxSection.Transactions);

            ofxDocument.StatementStart = xmlDocument.GetValue(xpath + "//DTSTART").ToDate();
            ofxDocument.StatementEnd   = xmlDocument.GetValue(xpath + "//DTEND").ToDate();

            var transactionNodes = xmlDocument.SelectNodes(xpath + "//STMTTRN");

            ofxDocument.Transactions = new List <Transaction>();

            if (transactionNodes == null)
            {
                return;
            }
            foreach (XmlNode node in transactionNodes)
            {
                ofxDocument.Transactions.Add(new Transaction(node, ofxDocument.Currency));
            }
        }
Example #2
0
        private OfxDocument Parse(string ofxString)
        {
            var ofx = new OfxDocument {
                AccType = GetAccountType(ofxString)
            };

            //Load into xml document
            var doc = new XmlDocument();

            doc.Load(new StringReader(ofxString));

            var currencyNode = doc.SelectSingleNode(GetXPath(ofx.AccType, OfxSection.Currency));

            if (currencyNode != null)
            {
                ofx.Currency = currencyNode.FirstChild.Value;
            }
            else
            {
                throw new OfxParseException("Currency not found");
            }

            //Get sign on node from OFX file
            var signOnNode = doc.SelectSingleNode(Resources.SignOn);

            //If exists, populate signon obj, else throw parse error
            if (signOnNode != null)
            {
                ofx.SignOn = new SignOn(signOnNode);
            }
            else
            {
                throw new OfxParseException("Sign On information not found");
            }

            //Get Account information for ofx xmlDocument
            var accountNode = doc.SelectSingleNode(GetXPath(ofx.AccType, OfxSection.AccountInfo));

            //If account info present, populate account object
            if (accountNode != null)
            {
                ofx.Account = new Account(accountNode, ofx.AccType);
            }
            else
            {
                throw new OfxParseException("Account information not found");
            }

            //Get list of transactions
            ImportTransations(ofx, doc);

            //Get balance info from ofx xmlDocument
            var ledgerNode    = doc.SelectSingleNode(GetXPath(ofx.AccType, OfxSection.Balance) + "/LEDGERBAL");
            var avaliableNode = doc.SelectSingleNode(GetXPath(ofx.AccType, OfxSection.Balance) + "/AVAILBAL");

            //If balance info present, populate balance object
            // ***** OFX files from my bank don't have the 'avaliableNode' node, so i manage a 'null' situation
            if (ledgerNode != null) // && avaliableNode != null
            {
                ofx.Balance = new Balance(ledgerNode, avaliableNode);
            }
            else
            {
                throw new OfxParseException("Balance information not found");
            }

            return(ofx);
        }