Is Linq-XML always so messy?

.net, c#, linq, linq-to-xml

Solution

Actually it would be better IMO to cast than to call `int.Parse`. Here's how I would write your query:

string id = itemId.ToString(); // We don't need to convert it each time!

var subset = from item in document.Descendants("Id")
             where item.Value == id
             let parent = item.Parent
             select new PurchaseItem
             {
                 Id = (int) parent.Element("Id"),
                 Name = (string) parent.Element("Name"),
                 Description = (string) parent.Element("Description"),
                 Price = (int) parent.Element("Price")
             };

Problem

``` var subset = from item in document.Descendants("Id") where item.Value == itemId.ToString() select new PurchaseItem() { Id = int.Parse(item.Parent.Element("Id").Value), Name = item.Parent.Element("Name").Value, Description = item.Parent.Element("Description").Value, Price = int.Parse(item.Parent.Element("Price").Value) }; ``` The structure of the XML is as follows: ``` <Items> <Item> <Id></Id> <Name></Name> <Description></Description> <Price></Price> </Item> </Items> ``` Id, and price are both integer values. Name and description are strings. I've found `Linq to XML` great for what I've used it for, this is just a snippet. But, on the other hand I get the feeling it should or could be cleaner. The casting seems the most obvious issue in this snippet. Any advice?

Original source