How to use LINQ to XML for receiving data in List format

use c#, .net 4.5

Yesterday i'm asking about how to read xml file. As yesterday was explained i can use Descendants() and Elements() for getting required elements of my file.

Now, i have xml file with some entities

<Library>
  <Book>
    <Author>Harrison FORD</Author>
    <Name>Unknown Name</Name>
    <Year>2000</Year>
   <Genre>Unknown Genre</Genre>
    <BookID>fc29b1cf-be41-4ed8-b101-5355396fef10</BookID>
  </Book>
  <Book>
    <Author>Unknown Author</Author>
    <Name>Unknown Name</Name>
    <Year>2000</Year>
    <Genre>Unknown Genre</Genre>
    <BookID>29b6d74b-71b3-4df6-af0b-d676ff5d23ad</BookID>
  </Book>
...
</Library>

What i want - to get list with Books (like List<Book>) where Book - its a separate entity with same properties like in xml file.

According to yesterday discussion try to do next

//get collection of all books
var myLib = from el in myDoc.Descendants("Library")
     select el.Element("Book");
     //create empty book
     Book addOneBook = new Book();
     //try to get each value and than save to just created book
     foreach (var books in myLib)
     {
     var myBook = from el in books.Descendants("Book")
          select el.Element("Author").Value.Trim();
      addBook.BookAuthor = myBook.ToString(); 
      ...
     //save
      myListWithBooks.Add(addOneBook);

I understand it like

enter image description here

During debuging found, that books in foreach is like on pic below - exactly what i want

enter image description here

But, when i try to get value of any element from - got not exactly requierd value - like

enter image description here

Think, i something miss. Where i'm wrong? Maybe i not all correctly understanding or something is misiing? Why in first time - all work like i thing, and in second same code return another result?

Aslo, maybe ther is some another way to get information that i need from this XML, but anyway - whant to know where i'm wrong now, with this code.


EDIT

enter image description here

Jon Skeet
people
quotationmark

You're calling ToString on a query - that's not on a single element.

Even if you fixed that, you'd add lots of references to the same object to your list... and this can all be done much more clearly with LINQ:

List<Book> books = myDoc.Root.Elements("Book")
                        .Select(x => new Book { 
                            Author = (string) x.Element("Author"),
                            Name = (string) x.Element("Name"),
                            Year = (int) x.Element("Year"),
                            Genre = (string) x.Element("Genre"),
                            Id = (string) x.Element("BookID")
                        })
                        .ToList();

(Note that I've changed the property name from BookAuthor to Author - it doesn't make sense to repeat the type name on the properties of that type. What other author would a Book have other than the book author?)

people

See more on this question at Stackoverflow