-1

我正在使用 C# 中的 LINQ to XML。我有以下代码,最后一行不断抛出 System.Exception: Value cannot be 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();

更改select el.Element("AuthName").Value)select (string)el.Element("AuthName")) 无济于事。

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

1 回答 1

1

更新

根据您的 xml,PrivateAccount元素没有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()
于 2013-01-06T21:33:24.790 回答