Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Linq-XML always so messy?

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?

like image 994
Finglas Avatar asked Jan 28 '10 22:01

Finglas


2 Answers

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")
             };
like image 138
Jon Skeet Avatar answered Sep 30 '22 14:09

Jon Skeet


I assume that you have an "Items" node as well?

You could do something like this, assuming that you are loading the document using XElement.Load()

var subset = from item in document.Elements("Item")
             where item.Element("Id").Value == itemId.ToString()
             select new PurchaseItem() {
                 Id = int.Parse(item.Element("Id").Value),
                 Name = item.Element("Name").Value,
                 Description = item.Element("Description").Value,
                 Price = int.Parse(item.Element("Price").Value)
             };

Not a lot better, but much easier to read!

like image 38
Mitchel Sellers Avatar answered Sep 30 '22 13:09

Mitchel Sellers