Solved

Linq query

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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