Linq to XML:XML查询(.Net Windows窗体应用程序) [英] Linq to XML: XML query (.Net Windows Form app)

查看:105
本文介绍了Linq to XML:XML查询(.Net Windows窗体应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个查询,该查询返回一个基于对客户ID的选择而包含客户信息的集合.当用户从组合框(cboCustomer)中选择一个客户时,选定的索引更改事件开始,并且下面的方法包含查询.不幸的是,该查询无法正常工作.当我将鼠标悬停在custRecord * 强文本 *中的结果上时,我收到错误消息:对象引用未设置为对象的实例."

I'm trying to build a query that returns a collection that contains a customer's information based upon the selection of a customer ID. When the user selects a customer from the combobox (cboCustomer), the selected index changed event kicks off and that method below contains the query. Unfortunately the query is not working. When I hover over the result in custRecord*strong text*, I receive the error, "Object reference not set to an instance of an object."

预先感谢从新手到XML!

Thanks in advance from a newbie to XML!

xml文件粘贴在下面;所有数据都完全组成了.

The xml file is pasted below; all data is totally made up.

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.Attribute("custid").Value == customerId
                select new
                {Fname=(string)account.Attribute("fname").Value,
                    Lname=(string)account.Attribute("lname").Value,
                    Ssn = (string)account.Attribute("ssn").Value,
                    Dob = (string)account.Attribute("dob").Value};
        }
    }

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

推荐答案

"custid"是"acct"的属性,而不是"account",因此您的Where子句永远找不到.因此,您的其他属性也是如此.

"custid" is an attribute of "acct", not "account", so your Where clause never finds anything. For that matter, the same is true of your other attributes.

这篇关于Linq to XML:XML查询(.Net Windows窗体应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆