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

  • Home
  • SEARCH
  • 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 6103419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:43:35+00:00 2026-05-23T13:43:35+00:00

I have this XML file that I parse into its elements and create a

  • 0

I have this XML file that I parse into its elements and create a list of a custom object Module.

XDocument kobra = XDocument.Load(new StringReader(results.OuterXml));
XNamespace ns = "#RowsetSchema";

var kobraNodeList = from s in kobra.Descendants(ns + "row")
                    select new Module
                    {
                         id = s.Attribute("ows_ID").Value,
                         name = s.Attribute("ows_Title").Value,
                         sourceFile = s.Attribute("ows_Source_x0020_Message_x0020_File_").Value,
                         scope = Scope.KOBRA,
                         component = string.Empty
                    };

and here’s my Module struct:

public struct Module
{
    public string name;
    public Scope scope;
    public string component;
    public int wordCound;
    public string id;
    public string sourceFile;
}

The code works fine, but things get weird when I try to convert the var kobraNodeList into a list of Modules, I get a System.NullReferenceException at the AddRange line:

this.moduleList = new List<Module>;
this.moduleList.AddRange(kobraNodeList);

When trying to debug, I notice that although kobraNodeList.Count() also returns System.NullReferenceException, a kobraNodeList.Any() returns true, and kobraNodeList.First() returns a perfectly valid and correct Module struct with the desired data.

The XML file is valid, and if I replace the linq query with this:

var kobraNodeList = from s in kobra.Descendants(ns + "row")
                    select  s;

I get a valid list of XElement, which I can Count() ok.
Can someone explain me what’s wrong? BTW, I’m using .NET 3.5.

  • 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-05-23T13:43:36+00:00Added an answer on May 23, 2026 at 1:43 pm

    That looks like one (or more) of kobra.Descendants has ows_ID, ows_Title or ows_Source_x0020_Message_x0020_File_ attribute missing.

    Linq uses deferred execution, so it won’t try to build the sequence until you ask for the items. When you call Any() or First(), it only needs the first item in the sequence to work, which tells me that the first item in kobra.Descendants does have all of the required nodes.

    However, one of the items after the first is probably missing at least one of those attributes – so you end up asking for the Value of a NULL attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that starts like this: <Elements name=Entities xmlns=XS-GenerationToolElements> I'll have
I have this XML file which I parse into an ArrayList In this ArrayList
I have a parser that parses XML file into SQLite database, and the current
I have the following code where i save the xml file into this particular
I have written a program that reads in a File object (really an XML
I have a weird XML file that I'm trying to parse. <Data> <Row> <Field
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
I have an xml file like this: <root> <item> <name>one</name> <status>good</status> </item> <item> <name>two</name>
I have an XML file, which I open in F# like this: let Bookmarks(xmlFile:string)
And its not '&' Im using the SAXParser object do parse the actual XML.

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.