How to read xml file using linq

evrm

New Member
I have this db.xml file\[code\]<items> <item> <title>Title1</title> <year>2013</title> <categories> <category>Category1</category> <category>Category2</category> <category>Category3</category> </categories> <count>10</count> </item> (and so on)</items>\[/code\]I read like that:\[code\]var items = from item in xdoc.Descendants("item") select new { Title = item.Element("title").Value, Year = item.Element("year").Value, Categories = item.Element("categories").Value, // I know this is wrong Count = item.Element("count").Value };\[/code\]The problem is how I can read the categories and add them to list?\[code\]foreach (var item in items){ book.Title = item.Title; book.Year = item.Year; foreach (var Category in Categories) { book.Categories.Add(Category); } book.Count = item.Count; books.Add(book);}\[/code\]
 
Back
Top