我的 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>
我的 LINQ 代码:
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();
}
问题:
我有一个客户类的通用列表。该类包含 3 个属性和另一个类 Account 的通用列表。我已经能够加载客户数据(id、fname、lname),但我不知道如何从 Accounts 子树加载任何数据 :(
代码给了我一个错误
System.Xml.Linq.dll 中发生“System.ArgumentNullException”类型的未处理异常 - 附加信息:值不能为空。
我一直在尝试代码的许多变体,但我无法使其工作:(有人可以向我发布一个如何加载帐户子树的工作代码吗?非常感谢!