2
votes

I´m trying for a day to make this work. I want to update a money attribute in my XML using LINQ to XML

My XML looks like this:

 <Bank>
   <Customer id="0">
     <FName>Adam</FName>
     <LName>Kruz</LName>
     <Accounts>
       <Account id="0" money="1500" />
       <Account id="1" money="6500" />
       <Account id="2" money="0" />
     </Accounts>
   </Customer>
   <Customer id="1">
     <FName>Benny</FName>
     <LName>Thoman</LName>
     <Accounts>
       <Account id="0" money="3200" />
     </Accounts>
   </Customer>
 </Bank>

I´ve beem trying with this code but with no success

 XDocument document = XDocument.Load("database.xml");

        XElement root = document.Root;

        root.Elements("Customer")
            .Where(e => e.Attribute("id").Value.Equals(customerID.ToString())).Select(e => e.Descendants("Account")
                .Where(f => f.Attribute("id").Value.Equals(this.id.ToString())).Select(f => f.Attribute("id")).First().SetValue(money.ToString()));


        document.Save("database.xml");

I´m getting an error: The type arguments for method 'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Pls tell me how do I edit an attribute in the subtree (Accounts) of an element (Account) thanks a lot :(

1

1 Answers

0
votes

Try this:

var query =
    from c in document.Root.Elements("Customer")
    where c.Attribute("id").Value == customerID.ToString()
    from a in c.Element("Accounts").Elements("Account")
    where a.Attribute("id").Value == this.id.ToString()
    select a;

query
    .First()
    .Attribute("money")
    .SetValue(money.ToString());