working with Invalid Cast handling When Parsing Nested XML with LINQ to XML in C#
I'm trying to implement I'm working on a personal project and I'm trying to parse a nested XML structure using LINQ to XML in C#, but I'm running into an `InvalidCastException` when accessing elements... Here's the XML I'm working with: ```xml <library> <book> <title>Effective C#</title> <author>Bill Wagner</author> <reviews> <review>Great book!</review> <review>Very insightful.</review> </reviews> </book> <book> <title>CLR via C#</title> <author>Jeffrey Richter</author> <reviews> <review>Must read for C# developers.</review> </reviews> </book> </library> ``` I'm using the following code to parse this XML and extract the titles and reviews: ```csharp var xml = XDocument.Parse(xmlString); var books = from book in xml.Descendants("book") select new { Title = book.Element("title")?.Value, Reviews = book.Element("reviews")?.Elements("review").Select(r => r.Value).ToList() }; ``` However, when I execute this code, I get the following behavior: ``` InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IEnumerable`1[System.String]'. ``` I've verified that the XML structure is valid and the `book.Element("reviews")` is indeed returning a collection of `review` elements. I've tried using `ToList()` directly on the `Elements` method as shown, but it doesn't seem to resolve the scenario. I suspect the question might be related to how I'm trying to access the `Value`. Is there a best practice when dealing with nested elements in LINQ to XML to avoid this kind of casting behavior? Any insights would be greatly appreciated! Any ideas what could be causing this? My team is using C# for this CLI tool. What are your experiences with this?