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?