views:

321

answers:

1

I have an xml file that looks like the following. What I'm trying to do is to create a query that selects only the items with the attribute "Channel" and the value "Automotive".

<item>
      <title>Industries</title>
      <category type="Channel">Automotive</category>
      <category type="Type">Cars</category>
      <category type="Token">Article</category>
      <category type="SpecialToken">News</category>
      <guid>637f0dd7-57a0-4001-8272-f0fba60feba1</guid>
</item>

Here is my code

 var feeds = (from item in doc.Descendants("item")
    where item.Element("category").Value == "Channel"  
    select new { }).ToList();

I tried using the item.attribute method but I can't get to the value within the Item, only the attribute Value of "type"

Could somebody please help me out on this?

Cheers, Chris

+3  A: 

I suspect you want:

var feeds = (from item in doc.Descendants("item")
             from category in item.Elements("category")
             where category.Value=="Automotive" && 
                   category.Attribute("type").Value == "Channel"
             select item).ToList();
Jon Skeet
I knew I had to do a subselect. Somehow I couldn't figure it out. Thanks again Jon.
Chris