Solved

Linq query

Posted on 2008-10-08
3
190 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 62

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 62

Expert Comment

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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word Template Mail merge with vb.net 4 40
c# code 19 61
Exception in Log4Net 1 20
Why is "packages element is not declared"? 2 44
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now