4

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!

2
  • 2
    change your your xml <Acount to <Account or change code c.Descendants("Acount") Commented Nov 10, 2012 at 23:09
  • thanks I was pretty tired when I wrote this :( Commented Nov 11, 2012 at 17:55

2 Answers 2

4

Your code works for me. But you have typing error in XML - "Acount" instead "Account"...

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah thanks. I found this out in morning :( it seems too much programing and little bit of sleep is not good for finding errors
1
var xDoc = XDocument.Load("myfile.xml");
var list = xDoc.Descendants("Customer")
                .Select(c => new
                {
                    Id=c.Attribute("id").Value,
                    FName = c.Element("FName").Value,
                    LName = c.Element("LName").Value,
                    Accounts = c.Descendants("Acount")
                                .Select(a => new{
                                    Id= a.Attribute("id").Value,
                                    Money = a.Attribute("money").Value,
                                })
                                .ToList()

                })
                .ToList();

PS: Since the tag name in your xml is Acount, I used the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.