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 6536185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:27:39+00:00 2026-05-25T10:27:39+00:00

I am new at using Linq to XML and have run across a rather

  • 0

I am new at using Linq to XML and have run across a rather troublesome error. When trying to pull in my XML file I get an error that reads “Object reference not set to an instance of an object.” and it is saying the error is because I am trying to use the select new statement. I have attached my code below:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Xml.Linq;
 public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {
     XDocument feed = XDocument.Load(Server.MapPath("VEHICLES.XML"));
     var query = from c in feed.Descendants("VEHICLES")
                 where (string) c.Element("VehicleType").Value == "0"
                 select new
                 {
                     Vin = c.Element("Vin").Value,
                     Status = c.Element("VehicleType").Value,
                     Year = c.Element("Year").Value
                 };

    CarLister.DataSource = query;
    CarLister.DataBind();
  }
}

the code works fine pulling in the all of the nodes using select c; instead of selectnew, but ideally I would like to only select certain pieces of information and there are a few entries that I would need to modify when pulling them in. I am not really sure what is causing this issue so any pointers or ideas on how to fix it would be greatly Appreciated, if you need any other info just ask!

  • 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-25T10:27:39+00:00Added an answer on May 25, 2026 at 10:27 am

    The problem is that you’re using the Value property everywhere – that will always fail if the result of foo.Element(...) returns null due to the element being missing. Fortunately, there’s a simple way round it – cast to string instead. The XElement explicit conversion to string returns null if the “input” is null, which is very handy.

    var query = from c in feed.Descendants("VEHICLES")
                where (string) c.Element("VehicleType") == "0"
                select new
                {
                    Vin = (string) c.Element("Vin"),
                    Status = (string) c.Element("VehicleType"),
                    Year = (string) c.Element("Year")
                };
    

    That will survive in the case that a <VEHICLES> element doesn’t have a <VehicleType>, <Vin>, <Status> or <Year> child.

    However, if this failure actually means the data was invalid to start with, you may want an exception anyway. It depends on how much you’re relying on the data to be sane to start with.

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

Sidebar

Related Questions

I am trying to read an XML file using LINQ. I have had no
I have an XML File that I am processing using LINQ. I want to
I have been experimenting with LINQ to XML and have run across a very
I have an xml string that I wish to traverse using LINQ to XML
I have a List<Item> collection that I am trying to generate an xml file
I have an issue while trying to parse an Xml to Objects using Linq
I have an xml file from which I am extracting html using LINQ to
Here's what I'm trying to do. I'm querying an XML file using LINQ to
Here's the problem I have a xml file that I am trying to append
I am writing a simple XML file parser using LINQ to XML. I want

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.