Ejemplo n.º 1
0
        /// <summary>
        /// Serialize a Transaction object to an XML writer.
        /// </summary>
        /// <param name="xmlWriter">XmlWriter to serialize to.</param>
        public void SerializeRecurringTransaction( XmlWriter xmlWriter, RecurringTransaction trans )
        {
            xmlWriter.WriteStartElement( "RecurringTransaction" );
            if( !trans.Enabled )
                xmlWriter.WriteAttributeString( "Enabled", "false" );

            xmlWriter.WriteWhitespace( Environment.NewLine );

            xmlWriter.WriteElementString( "StartingDate", trans.StartingDate.ToShortDateString() );
            xmlWriter.WriteWhitespace( Environment.NewLine );
            if( trans.EndingDate != DateTime.MaxValue )
            {
                xmlWriter.WriteElementString( "EndingDate", trans.EndingDate.ToShortDateString() );
                xmlWriter.WriteWhitespace( Environment.NewLine );
            }

            xmlWriter.WriteStartElement( "Interval" );
            if( trans.AddMonths > 0 )
                xmlWriter.WriteAttributeString( "Months", trans.AddMonths.ToString() );
            if( trans.AddDays > 0 )
                xmlWriter.WriteAttributeString( "Days", trans.AddDays.ToString() );
            xmlWriter.WriteEndElement();
            xmlWriter.WriteWhitespace( Environment.NewLine );

            SerializeTransactionBody( xmlWriter, trans );

            xmlWriter.WriteEndElement(); // </RecurringTransaction>
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserialize from an XML reader into a Transaction object.
        /// </summary>
        /// <param name="xmlReader">XmlReader to deserialize from.</param>
        /// <param name="ledger">Parent Ledger object.</param>
        /// <returns>A Transaction object.</returns>
        public RecurringTransaction DeserializeRecurringTransaction( XmlReader xmlReader, Version version, Ledger ledger )
        {
            if( xmlReader.NodeType != XmlNodeType.Element || xmlReader.Name != "RecurringTransaction" )
                throw new ApplicationException( "Needs a RecurringTransaction node to deserialize a RecurringTransaction object." );

            RecurringTransaction trans = new RecurringTransaction();

            if( string.Compare( xmlReader.GetAttribute( "Enabled" ), "false", StringComparison.OrdinalIgnoreCase ) == 0 )
                trans.Enabled = false;

            bool accountShortcut = false;
            LineItem lineItem = null;

            while( xmlReader.Read() )
            {
                Account account = null;
                Category category = null;
                int accountId = 0;
                decimal amount = 0;
                string memo = null;

                switch( xmlReader.NodeType )
                {
                    case XmlNodeType.Element:
                        switch( xmlReader.Name )
                        {
                            case "StartingDate":
                                trans.StartingDate = DateTime.Parse( xmlReader.ReadString() );
                                break;
                            case "Interval":
                                string attrib;
                                attrib = xmlReader.GetAttribute( "Months" );
                                if( attrib != null ) trans.AddMonths = Convert.ToInt32( attrib );
                                attrib = xmlReader.GetAttribute( "Days" );
                                if( attrib != null ) trans.AddDays = Convert.ToInt32( attrib );
                                break;
                            case "Date":
                                trans.Date = DateTime.Parse( xmlReader.ReadString() );
                                break;
                            case "Amount":
                                trans.Amount = Decimal.Parse( xmlReader.ReadString() );
                                break;
                            case "Description":
                                trans.Description = xmlReader.ReadString();
                                break;
                            case "Memo":
                                trans.Memo = xmlReader.ReadString();
                                break;
                            case "Credit":
                                if( accountShortcut )
                                    throw new ArgumentException( "LineItems not expected when using Account/Category elements." );
                                if( xmlReader.GetAttribute( "AccountId" ) != null )
                                {
                                    accountId = Int32.Parse( xmlReader.GetAttribute( "AccountId" ) );
                                    account = ledger.FindAccount( accountId );
                                }
                                else
                                {
                                    account = ledger.FindAccount( xmlReader.GetAttribute( "Account" ) );
                                    if( account == null )
                                        throw new ArgumentException( "Reference to undefined account named " + xmlReader.GetAttribute( "Account" ) );
                                    accountId = account.Id;
                                }
                                if( xmlReader.GetAttribute( "Category" ) != null )
                                    category = account.GetCategory( xmlReader.GetAttribute( "Category" ) );
                                if( xmlReader.GetAttribute( "Amount" ) != null )
                                    amount = Decimal.Parse( xmlReader.GetAttribute( "Amount" ) );
                                else
                                    amount = trans.Amount;

                                memo = xmlReader.GetAttribute( "Memo" );

                                lineItem = new LineItemCredit( account, amount, category );
                                lineItem.Transaction = trans;
                                lineItem.Memo = memo;
                                trans.LineItems.Add( lineItem );
                                break;
                            case "Debit":
                                if( accountShortcut )
                                    throw new ArgumentException( "LineItems not expected when using Account/Category elements." );
                                if( xmlReader.GetAttribute( "AccountId" ) != null )
                                {
                                    accountId = Int32.Parse( xmlReader.GetAttribute( "AccountId" ) );
                                    account = ledger.FindAccount( accountId );
                                }
                                else
                                {
                                    account = ledger.FindAccount( xmlReader.GetAttribute( "Account" ) );
                                    if( account == null )
                                        throw new ArgumentException( "Reference to undefined account named " + xmlReader.GetAttribute( "Account" ) );
                                    accountId = account.Id;
                                }
                                if( xmlReader.GetAttribute( "Category" ) != null )
                                    category = account.GetCategory( xmlReader.GetAttribute( "Category" ) );
                                if( xmlReader.GetAttribute( "Amount" ) != null )
                                    amount = Decimal.Parse( xmlReader.GetAttribute( "Amount" ) );
                                else
                                    amount = trans.Amount;

                                memo = xmlReader.GetAttribute( "Memo" );

                                lineItem = new LineItemDebit( account, amount, category );
                                lineItem.Transaction = trans;
                                lineItem.Memo = memo;
                                trans.LineItems.Add( lineItem );
                                break;
                            default:
                                break;
                        }
                        break;

                    case XmlNodeType.EndElement:
                        switch( xmlReader.Name )
                        {
                            case "RecurringTransaction":
                                return trans;
                        }
                        break;
                }
            }

            trans.SetUnmodified();

            return trans;
        }