C# LINQ到XML“System.Exception:值不能为null”

C# LINQ到XML“System.Exception:值不能为null”,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我正在使用C语言中的LINQ to XML。我有以下代码,最后一行一直抛出一个系统。异常:值不能为null。我想不出是什么问题。我什么都试过了。 AuthorizedToSign是一个列表。我能够使用一个庞大的嵌套foreach循环执行相同的操作。 我确信XML文件本身没有错误。 如果有人能帮忙,我将不胜感激 BusinessAccounts = (from a in accountRoot.Elements() where (bool)a.Element("Business") == true

我正在使用C语言中的LINQ to XML。我有以下代码,最后一行一直抛出一个系统。异常:值不能为null。我想不出是什么问题。我什么都试过了。 AuthorizedToSign是一个列表。我能够使用一个庞大的嵌套foreach循环执行相同的操作。 我确信XML文件本身没有错误。 如果有人能帮忙,我将不胜感激

BusinessAccounts = (from a in accountRoot.Elements()
where (bool)a.Element("Business") == true
select new BusinessAccount()
{
OpenDate = (DateTime)a.Element("DateOpened"),
Password = a.Element("Password").Value,
Balance = (double)a.Element("Balance"),
AccountStatus = (Status)Enum.Parse(typeof(Status), a.Element("Status").Value),
//Element AuthToSign has a collection of sub-elements called "authName"
//couldn't get the code below to work
AuthorizedToSign = (from el in a.Element("AuthorizedToSign").Elements()
                    select el.Element("AuthName").Value).ToList()
                                }).ToList();
更改选择el.ElementAuthName.Value 选择stringel.ElementAuthName的步骤 没用

XML文件中有许多条目如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Accounts>
  <BusinessAccount>
    <Business>true</Business>
    <AccountNumber>34534456</AccountNumber>
    <AccountBranchID>100</AccountBranchID>
    <AccountName>Elgris Tech</AccountName>
    <CompanyName>Elgris Tech</CompanyName>
    <CompanyID>235</CompanyID>
    <CreditLimit>50000</CreditLimit>
    <DateOpened>2014-12-13T00:00:00</DateOpened>
    <Balance>1200</Balance>
    <Password>1234</Password>
    <Status>Active</Status>
    <AuthorizedToSign>
      <AuthName>Yechiel</AuthName>
      <AuthName>Lev</AuthName>
      <AuthName>Roman</AuthName>
    </AuthorizedToSign>
  </BusinessAccount>
  <PrivateAccount>
    <Business>false</Business>
    <AccountNumber>34534458</AccountNumber>
    <AccountBranchID>100</AccountBranchID>
    <AccountName>Yechiel L.</AccountName>
    <CustomerName>Yechiel L.</CustomerName>
    <CustomerAddress>2sadfasosa, CA</CustomerAddress>
    <CustomerPhone>8-4268</CustomerPhone>
    <CardNumber>304456</CardNumber>
    <CreditLimit>10000</CreditLimit>
    <DateOpened>1994-06-23T00:00:00</DateOpened>
    <Balance>555000</Balance>
    <Password>pass</Password>
    <Status>Active</Status>
  </PrivateAccount>
</Accounts>
更新

根据xml,privateCount元素没有AuthorizedToSign节点,因此引用AuthorizedToSign节点会引发异常。因此,在您的情况下,解决方案很简单:

from a in accountRoot.Elements()
let authorized = a.Element("AuthorizedToSign")
where (bool)a.Element("Business")
select new BusinessAccount()
{
    OpenDate = (DateTime)a.Element("DateOpened"),
    Password = (string)a.Element("Password"),
    Balance = (double)a.Element("Balance"),
    AccountStatus = (Status)Enum.Parse(typeof(Status), (string)a.Element("Status")),
    AuthorizedToSign = authorized == null ? null : // or new List<string>()
                       authorized.Elements()
                                 .Select(auth => (string)auth)
                                 .ToList()
};
使用查询语法获取身份验证名称:

AuthorizedToSign = authorized == null ? null : // or new List<string>()
                   (from auth in authorized.Elements()
                    select (string)auth).ToList()

您试图读取的XML文件看起来像?也请显示完整的堆栈跟踪。我将猜测a.ElementStatus.Value为null,并且Enum.Parse正在引发该异常。我知道异常来自AuthorizedToSign行。我一行一行地测试了它Jon Skeet,我不知道怎么做,请告诉我在哪里找到它。我知道一个事实,异常来自授权的登录行。我一行一行地测试了它。@YechielLabunskiy抱歉,为该节点添加了检查,看起来您没有对某些元素进行授权登录。顺便说一句,您可以为这些元素提供默认值,例如Balance=Balance==null,而不是过滤掉没有某些元素(如“余额”)的帐户?0.0 : doublebalance@lazyberezovsky但是where boola.element业务应该过滤privateCount元素。@YechielLabunskiy抱歉,也解决了这个问题。原因-当您获得AuthorizedToSign元素时,这些元素已经是AuthName元素,您不需要获得el。ElementAuthName@YechielLabunskiy我添加了带有查询语法的示例。问题是相同的-尝试获取AuthName元素的AuthName元素:顺便说一句,如果一切正常,您可以接受答案: