Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

linq query c# returning null exception

Posted on 2011-10-05
5
Medium Priority
?
416 Views
Last Modified: 2013-11-11
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              
}
0
Comment
Question by:national_fulfillment
  • 2
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 36916325
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

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 36916328
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
                        });
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 36922311
Hi national_fulfillment;

The problem you are having is that each time the select clause is executed only one XElement is being processed and therefore all the other lines in the select clause but the tag that is being currently processed is valid. So the query would need to be like the following code snippet for it to work.

var myOrd = (from contactdetail in xDocument.Root.Descendants()
             select new
             {
                 TagName = contactdetail.Name,
                 Value = contactdetail.Value
              });
              
foreach (var item in myOrd)
{
    Console.WriteLine( item.TagName + " = " + item.Value );
}   

Open in new window


Now if you modify the schema of the XML file to this :

<contacts>
    <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>
</contacts>

Open in new window


Then modify your code a little bit like this :

var myOrd = (from contactdetail in xDocument.Root.Descendants("contact")
             select new
             {
                BillFirstName = contactdetail.Element("FirstName").Value,
                BillLastName = 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


Should return the needed results. Also the Tag names need to be the same as in the code and XML file, you had a couple with 1 at the end.

Fernando

0
 

Author Closing Comment

by:national_fulfillment
ID: 36931514
I can't change the structure of the xml so i used your first suggestion and it worked like a charm. Thanks a bunch.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 36931612

Not a problem, glad I was able to help.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question