Linq to XML:XML 查询结果未填充 DataGridView(.NET Windows 窗体应用程序)
我有点卡住了。我试图从查询 (Deposits
) 中获取结果来填充我的 DataGridView
控件 (dgvDeposits
)。我已经尝试过:
dgvDeposits.Datasource = Deposits
它不会产生错误,但 DataGridView
中没有填充任何数据。我期望的是存款列表:DepositID、DepositDate、DepositAmt。
这是代码:
private void btnDeposits_Click(object sender, EventArgs e)
{
var doc = XDocument.Load("Portfolio.xml");
var Deposits = from account in doc.Descendants("account")
let deps = account.Element("deposits")
let acct = account.Element("acct")
where (string)account.Attribute("custid").Value == customerId
select new
{
Depid = (string)account.Attribute("depid").Value,
Amount = (string)account.Attribute("depamount").Value,
Date = (string)account.Attribute("depdate").Value
};
}
这是 XML:
<?xml version="1.0" encoding="utf-8" ?>
<portfolio>
<account>
<acct custid="1" fname="Tommy" lname="Hawk"
ssn="928-329-1929" dob="4/6/1988"></acct>
<deposits depid="1000" depdate="1/2/2011" depamount="1350.53"></deposits>
<deposits depid="1003" depdate="2/3/2011" depamount="1377.81"></deposits>
<deposits depid="1008" depdate="3/14/2011" depamount="84.00"></deposits>
<withdrawals wdid="2001" wddate="1/31/2011" wdamount="80.00"></withdrawals>
<withdrawals wdid="2005" wddate="4/8/2011" wdamount="80.00"></withdrawals>
<withdrawals wdid="2007" wddate="6/1/2011" wdamount="2600.00"></withdrawals>
</account>
<account>
<acct custid="2" fname="I. P." lname="Nightly"
ssn="457-23-4871" dob="6/1/1945"></acct>
<deposits depid="1004" depdate="2/8/2011" depamount="741.22"></deposits>
<deposits depid="1005" depdate="2/9/2011" depamount="47.00"></deposits>
<deposits depid="1009" depdate="3/14/2011" depamount="89.99"></deposits>
<withdrawals wdid="2003" wddate="3/1/2011" wdamount="55.00"></withdrawals>
<withdrawals wdid="2004" wddate="3/3/2011" wdamount="28.00"></withdrawals>
<withdrawals wdid="2006" wddate="4/8/2011" wdamount="450.00"></withdrawals>
</account>
<account>
<acct custid="3" fname="Mary" lname="Echmass"
ssn="192-01-2933" dob="8/10/1973"></acct>
<deposits depid="1002" depdate="1/15/2011" depamount="841.77"></deposits>
<deposits depid="1006" depdate="2/14/2011" depamount="2170.00"></deposits>
<deposits depid="1007" depdate="3/10/2011" depamount="21.01"></deposits>
<withdrawals wdid="2002" wddate="1/16/2011" wdamount="700.00"></withdrawals>
<withdrawals wdid="2008" wddate="6/3/2011" wdamount="24.00"></withdrawals>
<withdrawals wdid="2009" wddate="6/30/2100" wdamount="38.46"></withdrawals>
</account>
</portfolio>
I'm kinda stuck. I'm trying to get the results from my query (Deposits
) to populate my DataGridView
control (dgvDeposits
). I've tried:
dgvDeposits.Datasource = Deposits
It doesn't produce an error but no data is populated in the DataGridView
. What I expect is a list of deposits: DepositID, DepositDate, DepositAmt.
Here is the code:
private void btnDeposits_Click(object sender, EventArgs e)
{
var doc = XDocument.Load("Portfolio.xml");
var Deposits = from account in doc.Descendants("account")
let deps = account.Element("deposits")
let acct = account.Element("acct")
where (string)account.Attribute("custid").Value == customerId
select new
{
Depid = (string)account.Attribute("depid").Value,
Amount = (string)account.Attribute("depamount").Value,
Date = (string)account.Attribute("depdate").Value
};
}
And here is the XML:
<?xml version="1.0" encoding="utf-8" ?>
<portfolio>
<account>
<acct custid="1" fname="Tommy" lname="Hawk"
ssn="928-329-1929" dob="4/6/1988"></acct>
<deposits depid="1000" depdate="1/2/2011" depamount="1350.53"></deposits>
<deposits depid="1003" depdate="2/3/2011" depamount="1377.81"></deposits>
<deposits depid="1008" depdate="3/14/2011" depamount="84.00"></deposits>
<withdrawals wdid="2001" wddate="1/31/2011" wdamount="80.00"></withdrawals>
<withdrawals wdid="2005" wddate="4/8/2011" wdamount="80.00"></withdrawals>
<withdrawals wdid="2007" wddate="6/1/2011" wdamount="2600.00"></withdrawals>
</account>
<account>
<acct custid="2" fname="I. P." lname="Nightly"
ssn="457-23-4871" dob="6/1/1945"></acct>
<deposits depid="1004" depdate="2/8/2011" depamount="741.22"></deposits>
<deposits depid="1005" depdate="2/9/2011" depamount="47.00"></deposits>
<deposits depid="1009" depdate="3/14/2011" depamount="89.99"></deposits>
<withdrawals wdid="2003" wddate="3/1/2011" wdamount="55.00"></withdrawals>
<withdrawals wdid="2004" wddate="3/3/2011" wdamount="28.00"></withdrawals>
<withdrawals wdid="2006" wddate="4/8/2011" wdamount="450.00"></withdrawals>
</account>
<account>
<acct custid="3" fname="Mary" lname="Echmass"
ssn="192-01-2933" dob="8/10/1973"></acct>
<deposits depid="1002" depdate="1/15/2011" depamount="841.77"></deposits>
<deposits depid="1006" depdate="2/14/2011" depamount="2170.00"></deposits>
<deposits depid="1007" depdate="3/10/2011" depamount="21.01"></deposits>
<withdrawals wdid="2002" wddate="1/16/2011" wdamount="700.00"></withdrawals>
<withdrawals wdid="2008" wddate="6/3/2011" wdamount="24.00"></withdrawals>
<withdrawals wdid="2009" wddate="6/30/2100" wdamount="38.46"></withdrawals>
</account>
</portfolio>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仍然没有修复 最后一个问题中出现的错误:您必须在
where
和select
子句中使用acct
,而不是account
。You still didn't fix the error you had in your last question: you have to use
acct
, notaccount
in yourwhere
andselect
clauses.您应该在行中使用 acct 而不是 account,并在字段初始值设定项中使用 dept 而不是 account。
此外,我怀疑它只会返回具有所需 custid 值的每个 acct 元素的第一个 deposit 元素,因此总共可能只有一个元素。
为了返回所有它们,您可以使用如下查询:
You should use acct instead of account in where line and dept instead of account in the field initializer.
Moreover, I suspect it will return only the first deposit element for each acct element with the desired custid value, so probably only one element in total.
In order to return them all you can use a query like this: