2

私はこの仕事をするために一日を努力しています。LINQtoXMLを使用してXMLのmoney属性を更新したい

私のXMLは次のようになります。

 <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>

私はこのコードを試してみましたが、成功しませんでした

 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");

エラーが発生しました:メソッド'System.Linq.Enumerable.Select(System.Collections.Generic.IEnumerable、System.Func)'の型引数を使用法から推測できません。タイプ引数を明示的に指定してみてください。

plsは要素(アカウント)のサブツリー(アカウント)の属性を編集する方法を教えてくれますありがとうございます:(

4

1 に答える 1

0

これを試して:

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());
于 2012-11-12T10:49:36.553 に答える