XML file looks like this:
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</catalog>
I need to read all data to my class:
public class Book
{
public string Id { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public Genre Genre { get; set; }
public decimal Price { get; set; }
public DateTime PublishDate { get; set; }
public string Description { get; set; }
}
Below code doesn't work, I can grab bookId from node, but not Title, Author.. How to achieve that result? This is what I have so far:
const string filePath = @"C:\Users\Michał\Desktop\books.xml";
XDocument xmlDoc = XDocument.Load(filePath);
var dupa = xmlDoc
.Descendants("book")
.Select(x => new Book()
{
Id = (string) x.Attribute("bookid"),
Title = (string) x.Attribute("title") // Title is empty after that code runs
}).ToList();