My XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Bank>
<Customer id="0">
<FName>Adam</FName>
<LName>Kruz</LName>
<Accounts>
<Acount id="0" money="1500" />
<Acount id="1" money="6500" />
</Accounts>
</Customer>
</Bank>
My LINQ code:
private void loadCustomers()
{
customers =
(
from c in XDocument.Load("database.xml").Root.Descendants("Customer")
select
new Customer((int) c.Attribute("id"), (string) c.Element("FName"), (string) c.Element("LName"))
{
accounts =
(
from a in c.Descendants("Account")
select new Account((int) a.Attribute("id"))
{
money = (double) a.Attribute("money")
}
).ToList()
}
).ToList();
}
Problem:
I have a generic list of class Customer. That class contains 3 properties and another generic list of class Account. I've been able to load Customer data (id, fname, lname) but I dont know how to load any data from Accounts sub tree :(
code gives me an error
An unhandled exception of type 'System.ArgumentNullException' occurred in System.Xml.Linq.dll - Additional information: Value cannot be null.
I've been trying many variants of the code and I just could not make it work :( Can someone post me a working code how to load Accounts subtree ? Thanks a lot!
<Acountto<Accountor change codec.Descendants("Acount")