2013-01-06 32 views
-1

我在C#中使用LINQ to XML。我有下面的代碼,最後一行不斷拋出一個System.Exception:值不能爲空。我無法弄清楚問題所在。我已經嘗試了一切。 AuthorizedToSign是一個列表。我能夠使用大型嵌套的foreach循環執行相同的操作。 我知道肯定沒有錯誤,只要XML文件本身。 如果有人能幫助,我將不勝感激。LINQ to XML「System.Exception:值不能爲空」

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> 
+3

,並且您試圖讀取看起來像XML文件...? –

+1

請顯示完整的堆棧跟蹤。 –

+1

我要猜測並說'a.Element(「Status」)。Value'是'null','Enum.Parse'則拋出這個異常。 –

回答

1

UPDATE

根據你的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() 
+0

我知道這個異常來自AuthorizedToSign行。我一行一行地測試它。 –

+0

@YechielLabunskiy對不起,還爲該節點添加了檢查(看起來您對某些元素沒有'AuthorizedToSign')。順便說一句,而不是篩選出沒有一些元素的帳戶(例如'平衡'),您可以爲這些元素提供默認值(例如'Balance =(balance == null)?0.0:(double)balance') –

+0

@lazyberezovsky但是'where(bool)a.Element(「Business」)'應該過濾'PrivateAccount'元素。 –