Linq to XML:.Net Windows 窗体、XML 查询

发布于 2024-12-11 20:42:33 字数 3144 浏览 0 评论 0原文

我正在尝试创建一个客户记录,其中包括名字、姓氏、SSN 和出生日期。当用户从组合框 (cboCustomer) 中选择客户时,它会调用 SelectedIndexChanged 事件。我想使用选定的索引从 XML 中获取正确的客户记录。

这是我创建的查询,但它是空的。查询下方是我的 XML 数据源。

    private void cboCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        var doc = XElement.Load("Portfolio.xml");
        customerId = cboCustomer.SelectedIndex.ToString();
        if (cboCustomer.SelectedIndex != 0)
        {
            var custRecord =
                from account in doc.Descendants("account")
                let acct = account.Element("acct")
                where (string)account.Element("custid") == customerId
                select new
                {Fname=(string)account.Element("fname"),
                    Lname = (string)account.Element("lname"),
                    Ssn = (string)account.Element("ssn"),
                    Dob = (string)account.Element("dob")
                };
        }
    }
<?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 am trying to create a customer record which would include First Name, Last Name, SSN and Date of Birth. When a user selects a customer from the combobox (cboCustomer), it calls the SelectedIndexChanged event. I want to use the selected index to get the correct customer record from my XML.

Here is the query I've created, but it is empty. Below the query is my XML data source.

    private void cboCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        var doc = XElement.Load("Portfolio.xml");
        customerId = cboCustomer.SelectedIndex.ToString();
        if (cboCustomer.SelectedIndex != 0)
        {
            var custRecord =
                from account in doc.Descendants("account")
                let acct = account.Element("acct")
                where (string)account.Element("custid") == customerId
                select new
                {Fname=(string)account.Element("fname"),
                    Lname = (string)account.Element("lname"),
                    Ssn = (string)account.Element("ssn"),
                    Dob = (string)account.Element("dob")
                };
        }
    }
<?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倥絔 2024-12-18 20:42:33

您的 XML 中没有元素 custid,但您正在代码中查找它。

您可能想要

where acct.Attribute("custid").Value == customerId

并且需要以类似的方式修改您的 select

There is no element custid in your XML, but you're looking for it in your code.

You probably want

where acct.Attribute("custid").Value == customerId

And you need to modify your select in a similar way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文