Solved

Linq query

Posted on 2008-10-08
3
208 Views
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)
              MessageBox.Show(element.ToString());


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

any help?
0
Comment
Question by:sansoftura
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 22669657
Hi sansoftura;

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

Fernando

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

0
 
LVL 6

Author Closing Comment

by:sansoftura
ID: 31504250
Thanks
0
 
LVL 63

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 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