2009-02-10 86 views
3

我有一个如下所示的xml文件。我想要做的是创建一个查询,只选择具有属性“通道”和值“汽车”的项目。使用linq读取属性值

<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> 

这里是我的代码

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

我使用item.attribute方法试过,但我不能在项目中得到的价值,只有“类”

的属性值

请问有人能帮我解决这个问题吗?

干杯, 克里斯

回答

10

我怀疑你想:

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(); 
+0

我知道我必须做一个子查询中。不知何故,我无法弄清楚。再次感谢乔恩。 – Chris 2009-02-10 16:39:04