Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9086607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:31:48+00:00 2026-06-16T21:31:48+00:00

I am working with LINQ to XML in C#. I have the following code,

  • 0

I am working with LINQ to XML in C#. I have the following code, the last line keeps throwing a System.Exception: Value cannot be null. I can’t figure out what the problem is. I’ve tried everything.
AuthorizedToSign is a List. I was able to perform the same action using a bulky nested foreach loop.
I know for sure there are no errors as far as the XML file itself.
If anyone can help I would greatly appreciate it.

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();

changing select el.Element("AuthName").Value)
to select (string)el.Element("AuthName"))
doesn’t help.

The XML file has a lot of entries that look like this:

<?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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:31:49+00:00Added an answer on June 16, 2026 at 9:31 pm

    UPDATE

    According to your xml, PrivateAccount element does not have AuthorizedToSign node, thus referencing to AuthorizedToSign node throws an exception. So, in your case solution will be simple:

    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()
    };
    

    Getting auth names with query syntax:

    AuthorizedToSign = authorized == null ? null : // or new List<string>()
                       (from auth in authorized.Elements()
                        select (string)auth).ToList()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im working with xml and linq. I have 2 xml files both contain ID
I have the following code in C# and .net 3.5 that is working fine
What I have Tried 1. Adding reference to , System.Core, System.Xml.Linq, System.Data.Linq by right
Order by descending is not working on LINQ to Entity In the following Query
I'm working on the following LINQ query: public void GetAuditRuleAgencyRecords(IEnumerable<Entities.AuditRule> rules) { using (LinqModelDataContext
I have started using Linq to SQL for a project im working on and
I have the following mapping in my Castle Windsor xml file which has worked
I am working on my first NHibernate project, and have the following setup/requirement. I
Possible Duplicate: Use Linq to Xml with Xml namespaces I have an xml: <?xml
I am working at Windows Phone 7 C#, XAML, XML and LINQ programming. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.