Click here to Skip to main content
15,905,325 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a bit of challenge which I have tried to google and find similar problems. I have tried some of the solutions but still unable to get it solve so I decided to open a new thread.
The issue now is if I try to print out the items inside pain.transaction, I get one only items for multiple records which isn't supposed to be so. How do I go round this? I can't also change how I read the xml file. I have to stick to this approach.

What I have tried:

    List<Transactions> tl = new List<Transactions>(); 
    Transactions t = new Transactions();
    XmlNodeList details = xmldoc.GetElementsByTagName("Dtl");
    foreach (XmlNode crdNode in details)
    {
        XmlNode CdtName = crdNode["Nm"];

        t.Name= Name.ChildNodes.Item(0).InnerText;
        //If i print out t.Name, it displays all the names.

        XmlNode CdtAddress = crdNode["PstlAdr"];

        t.Address=CdtAddress.ChildNodes.Item(0).InnerText;

    }
    XmlNodeList pmtId = xmldoc.GetElementsByTagName("ID");
    foreach (XmlNode Pm in pmtId)
    {
        XmlNode transaction = Pm["Ref"];
        t.TransactionReference =transaction.ChildNodes.Item(0).InnerText;
    }
    XmlNode instruct = Pm["Instr"];
    t.Instruction =instruct.ChildNodes.Item(0).InnerText;
}
tl.Add(t);
pain.transaction = tl; //pain.transaction contains a list of transaction
Posted
Updated 7-Dec-17 22:27pm
v3

I've had a look at the code snip it and you appear to be missing some parentheses. Either way the issue seems simple.

You only adding the item "t" once after your loop. You need to include the "tl.Add(t)" inside the loop.
 
Share this answer
 
You only ever create a single Transactions object and repeatedly set new values into it. You then add that to your List<Transactions> list so that is all you have. You need to create a new Transactions object for each XML node.
 
Share this answer
 
Comments
Mcbaloo 10-Dec-17 0:58am    
Sorry I'm just commenting. I have done what you said. But is there a way to access the objects I created inside the transaction object?. I created a details object to store the CdtName and Cdt address. Then have the details inside transactions. I want to get CdtName from transactions without having to use details object directly
Richard MacCutchan 10-Dec-17 3:51am    
Assuming your Transactions class members are public (which it looks like they are) then yes, you can extract them by referring to them just the same way as when you set their values.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900