Linq query

Posted on 2008-10-08
Medium Priority
Last Modified: 2013-12-17
I am trying to get elements from an XML that i created using XDocument.

 XDocument books = new XDocument(
                                    new XElement("Books",
                                new XElement("book",
                                    new XElement("name", "Book 1"),
                                    new XElement("author", "John Doe")),
                                new XElement("book",
                                    new XElement("name", "Book 2"),
                                    new XElement("author", "Jane Doe"))));

IEnumerable<XElement> elements = books.Element("books").Elements("book");

foreach (XElement element in elements)

I get an error at the line IEnumerable<XElement> elements "Object reference not set to an instance of an object."

any help?
Question by:sansoftura
  • 2
LVL 64

Accepted Solution

Fernando Soto earned 2000 total points
ID: 22669657
Hi sansoftura;

Small type O in the line of code, see snippet.


// The node name books in this line of code
IEnumerable<XElement> elements = books.Element("books").Elements("book");
// Should be Books like in the following line of code.
IEnumerable<XElement> elements = books.Element("Books").Elements("book");

Open in new window


Author Closing Comment

ID: 31504250
LVL 64

Expert Comment

by:Fernando Soto
ID: 22670204
Not a problem, glad I was able to help.  ;=)

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

588 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