Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6744559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:05:54+00:00 2026-05-26T12:05:54+00:00

There are 3 deposits in my XML file, but the query is only returning

  • 0

There are 3 deposits in my XML file, but the query is only returning the first deposit 3 thime. I have pastd in the Customer class and the XML file. This is a .Net 4 windows forms application. Deposits are shown on the form when the user clicks a ‘DEPOSITS’ button.

User Inerface form method that calls Customer Class:

    private void btnDeposits_Click(object sender, EventArgs e)
    {
        if (customerId == "0")
        {
            MessageBox.Show("Please first select a customer", "Error");
            return;
        }

        BL_Customer oCustomer = new BL_Customer();

        try
        {
            //Call method to pull data from XML
            oCustomer.getDeposits(customerId);
        }
        catch (Exception ex)
        {
            tssErrors.Text = ex.Message;
        }

        //Set datagridview datasource and format the view Headers & Cells
        dgvDeposits.DataSource = oCustomer.Deposits;
        dgvDeposits.Columns[0].HeaderText = "Deposit ID";
        dgvDeposits.Columns[1].HeaderText = "Amount";
        dgvDeposits.Columns[2].HeaderText = "Date of Deposit"; 
        dgvDeposits.Columns[0].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dgvDeposits.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dgvDeposits.Columns[2].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        dgvDeposits.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
        dgvDeposits.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
        dgvDeposits.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
        dgvDeposits.Columns[1].DefaultCellStyle.Format = "c2";
        dgvDeposits.Columns[0].Width = 50;
        dgvDeposits.Columns[1].Width = 60;
        dgvDeposits.Columns[2].Width = 80;
    }

CUSTOMER class

   public BL_Customer()
    {
        oDeposits = new List<BL_Deposit>();
    }

    public List<BL_Deposit> addDeposits(BL_Deposit oDeposit)
    {
        oDeposits.Add(oDeposit);
        return oDeposits;
    }

   public double  getDeposits(string customerId)
    {
        double sumDep = 0;
        //Returns list of deposits for selected customer
        var doc = XDocument.Load("Portfolio.xml");
        var Deposits = from account in doc.Descendants("account")
                       from deposit in account.Elements("deposits")
                       where (string)account.Element("acct").Attribute("custid").Value == customerId
                       select new
                       {
                           Depid = (string)deposit.Attribute("depid").Value,
                           Amount = (string)deposit.Attribute("depamount").Value,
                           Date = (string)deposit.Attribute("depdate").Value
                       };

                        //Loop through deposits and add a deposit object
                        BL_Deposit oDeposit = new BL_Deposit();
                        for (int i = 0; i < 3; i++)
                        {
                            oDeposit.DepAmt = Convert.ToDouble(Deposits.ElementAt(i).Amount);
                            oDeposit.DepDate = Convert.ToDateTime(Deposits.ElementAt(i).Date);
                            oDeposit.DepositId = Convert.ToInt32(Deposits.ElementAt(i).Depid);
                            addDeposits(oDeposit);
                            sumDep += oDeposit.DepAmt;
                        }
                        return sumDep;
        }

XML File

<?xml version="1.0" encoding="utf-8" ?>
<portfolio>
  <account>
    <acct custid="1" fname="Tommy" lname="Hawk" ssn="928-329-1929" dob="4/6/1988"></acct>
    <deposits depid="1000" depdate="1/2/2011" depamount="1350.53"></deposits>
    <deposits depid="1003" depdate="2/3/2011" depamount="1377.81"></deposits>
    <deposits depid="1008" depdate="3/14/2011" depamount="84.00"></deposits>
    <withdrawals wdid="2001" wddate="1/31/2011" wdamount="80.00"></withdrawals>
    <withdrawals wdid="2005" wddate="4/8/2011" wdamount="80.00"></withdrawals>
    <withdrawals wdid="2007" wddate="6/1/2011" wdamount="2600.00"></withdrawals>
  </account>
  <account>
    <acct custid="2" fname="I. P." lname="Nightly" ssn="457-23-4871" dob="6/1/1945"></acct>
    <deposits depid="1004" depdate="2/8/2011" depamount="741.22"></deposits>
    <deposits depid="1005" depdate="2/9/2011" depamount="47.00"></deposits>
    <deposits depid="1009" depdate="3/14/2011" depamount="89.99"></deposits>
    <withdrawals wdid="2003" wddate="3/1/2011" wdamount="55.00"></withdrawals>
    <withdrawals wdid="2004" wddate="3/3/2011" wdamount="28.00"></withdrawals>
    <withdrawals wdid="2006" wddate="4/8/2011" wdamount="450.00"></withdrawals>
  </account>
  <account>
    <acct custid="3" fname="Mary" lname="Echmass" ssn="192-01-2933" dob="8/10/1973"></acct>
    <deposits depid="1002" depdate="1/15/2011" depamount="841.77"></deposits>
    <deposits depid="1006" depdate="2/14/2011" depamount="2170.00"></deposits>
    <deposits depid="1007" depdate="3/10/2011" depamount="21.01"></deposits>
    <withdrawals wdid="2002" wddate="1/16/2011" wdamount="700.00"></withdrawals>
    <withdrawals wdid="2008" wddate="6/3/2011" wdamount="24.00"></withdrawals>
    <withdrawals wdid="2009" wddate="6/30/2100" wdamount="38.46"></withdrawals>
  </account>  
</portfolio>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:05:55+00:00Added an answer on May 26, 2026 at 12:05 pm

    Well you create a single object with BL_Deposit oDeposit = new BL_Deposit();, then in your loop you change the properties of the same object. Move the code with the creation of the object into the loop, then I think you should get what you want.

    Of course you could create the objects directly in the query, avoiding the anonymous objects.

    [edit]
    Here is how you can use LINQ to directly create your List:

    List<BL_Deposit> deposits =
      (from account in doc.Descendants("account")
    from deposit in account.Elements("deposits")
    where (string)account.Element("acct").Attribute("custid").Value == customerId
    select new BL_Deposit() 
    {
        DepositId = (int)deposit.Attribute("depid"),
        DepAmt = (double)deposit.Attribute("depamount"),
        DepDate = Convert.ToDateTime(deposit.Attribute("depdate").Value)
    }).ToList();
    

    Then you can also compute the sum with e.g. sumDep = deposits.Sum(d => d.DepAmt);.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table named transaction with transaction_date, deposit, withdrawal fields. There
There are two popular closure styles in javascript. The first I call anonymous constructor
There are a few ways to get class-like behavior in javascript, the most common
There are numerous Agile software development methods. Which ones have you used in practice
I'm having troubles with the logic and would apprecite any help/tips. I have <Deposits>
I have searched quite thoroughly on this but can't seem to find an answer.
I have a database that contains a table of deposits (security deposits, pet deposits,
There is this strange situation I'm fighting on. I have 3 pages, les call
I'm trying to make a crosstab query (with access tables), But I got lost
I have a query in my DAL that results 1 item, a date. It's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.