-1

I am trying to read an XML file with LINQ for the first time. I have always used old styles to read XML files.

My sample XML is like below, there are more tag but no need to display them here...

<Person>
  <person_id_external>370</person_id_external>
  <country_of_birth>ZAF</country_of_birth>
</Person>

and my c# code is below

// filePath is correct and it can read xml file perfectly...
XDocument xdoc = XDocument.Load(filePath);

var People = from Person in xdoc.Descendants("Person")
             select new
             {
                  person_id_external = Person.Descendants("person_id_external"),
                  country_of_birth = Person.Descendants("country_of_birth")
             };

// IT THROWS "Object reference not set to an instance of an object." ****
MessageBox.Show(People.ToList()[0].person_id_external + " " + People.ToList()[0].country_of_birth);

It throws a null exception when it hits the MessageBox.Show()....

what am I doing wrong and how can I fix this?

Arif YILMAZ
  • 5,754
  • 26
  • 104
  • 189

3 Answers3

1

I understand this is just testing code, but you don't want to ToList() multiple times. Do it once and then you can index into the collection:

var People = (from Person in xdoc.Descendants("Person")
         select new
         {
              person_id_external = Person.Element("person_id_external").Value,
              country_of_birth = Person.Element("country_of_birth").Value
         }).ToList();
Crowcoder
  • 11,250
  • 3
  • 36
  • 45
1

Here you go:

XDocument xdoc = XDocument.Load(filePath);

var people = from person in xdoc.Descendants("Person")
    select new
    {
        person_id_external = person.Element("person_id_external").Value,
        country_of_birth = person.Element("country_of_birth").Value
    };

if (people.Any())
{
    var person = people.First();
    MessageBox.Show(person.person_id_external + " " + person.country_of_birth);
}
Shahzad Qureshi
  • 1,776
  • 14
  • 15
1

It must return null either because of your xml file data or the data structure within your xml file. Here is a working sample.

string xml = "<Person><person_id_external>370</person_id_external><country_of_birth>ZAF</country_of_birth></Person>";
XDocument xdoc = XDocument.Parse(xml);

var People = from Person in xdoc.Descendants("Person")
                select new
                {
                    person_id_external = Person.Element("person_id_external").Value,
                    country_of_birth = Person.Element("country_of_birth").Value
                };

MessageBox.Show(People.ToList()[0].person_id_external + " " + People.ToList()[0].country_of_birth);

I have used Person.Element to get the value of person_id_external and country_of_birth

The only difference between this code and yours is how the information is being loaded into the XDocument. I have loaded it from a string and you are loading it from a file. Your null exception could only be due to the data structure of your xml file then.

Praveen Paulose
  • 5,741
  • 1
  • 15
  • 19