Link to home
Start Free TrialLog in
Avatar of national_fulfillment
national_fulfillmentFlag for United States of America

asked on

linq query c# returning null exception

I'm still pretty new to using Linq but i've used a query like this in the past and it works. When i debug the program i can see data in the document object but when it hits the linq statement i get a null exception. The document structure looks like this:
<contact>
      <FirstName>Anne</FirstName>
      <LastName>Smith</LastName>
      <Address1>729 Main Ave</Address1>
      <Address2/>
      <City>Grand Haven</City>
      <State>MI</State>
      <Zip>49417</Zip>
      <Phone>6168500344</Phone>
</contact>

THis is my query:
var myOrd = (from contactdetail in xDocument.Root.Descendants()
                       select new
                          {
                          BillFirstName = contactdetail.Element("FirstName").Value,
                          BillLastName = contactdetail.Element("LastName").Value,
                          BillStreet1 = contactdetail.Element("Address1").Value,
                          BillStreet2 = contactdetail.Element("Address2l").Value,
                          BillCity = contactdetail.Element("Cityl").Value,
                          BillState = contactdetail.Element("Statel").Value,
                          BillZip = contactdetail.Element("Zipl").Value,
 } );
             foreach (var type in myOrd)
           {
//do stuff here              
}
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Try the following query
var myOrd = (from contactdetail in xDocument.Descendants("contact")
                         select new
                         {
                            BillFirstName = contactdetail.Descendants("FirstName").Value,
                            BilllLastName = contactdetail.Element("LastName").Value,
                            BillStreet1 = contactdetail.Element("Address1").Value,
                            BillStreet2 = contactdetail.Element("Address2").Value,
                            BillCity = contactdetail.Element("City").Value,
                            BillState = contactdetail.Element("State").Value,
                            BillZip = contactdetail.Element("Zip").Value
                        });

Open in new window

Copy pase issue

this is correct:

            var myOrd = (from contactdetail in xDocument.Descendants("contact")
                         select new
                         {
                             BillFirstName = contactdetail.Element("FirstName").Value,
                            BilllLastName = contactdetail.Element("LastName").Value,
                            BillStreet1 = contactdetail.Element("Address1").Value,
                            BillStreet2 = contactdetail.Element("Address2").Value,
                            BillCity = contactdetail.Element("City").Value,
                            BillState = contactdetail.Element("State").Value,
                            BillZip = contactdetail.Element("Zip").Value
                        });

or

            var myOrd = (from contactdetail in xDocument.Descendants("contact")
                         select new
                         {
                             BillFirstName = contactdetail.Descendants("FirstName").First().Value,
                            BilllLastName = contactdetail.Element("LastName").Value,
                            BillStreet1 = contactdetail.Element("Address1").Value,
                            BillStreet2 = contactdetail.Element("Address2").Value,
                            BillCity = contactdetail.Element("City").Value,
                            BillState = contactdetail.Element("State").Value,
                            BillZip = contactdetail.Element("Zip").Value
                        });
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of national_fulfillment

ASKER

I can't change the structure of the xml so i used your first suggestion and it worked like a charm. Thanks a bunch.

Not a problem, glad I was able to help.