?
Solved

Linq query

Posted on 2008-10-08
3
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

762 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