【问题标题】:Read attributes values with linq使用 linq 读取属性值
【发布时间】:2009-02-10 15:32:09
【问题描述】:

我有一个如下所示的 xml 文件。我要做的是创建一个查询,该查询仅选择具有“Channel”属性和“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>

这是我的代码

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

我尝试使用 item.attribute 方法,但无法获取 Item 中的值,只能获取“type”的属性值

有人可以帮我解决这个问题吗?

干杯, 克里斯

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

    我怀疑你想要:

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

    【讨论】:

    • 我知道我必须进行子选择。不知怎的,我无法弄清楚。再次感谢乔恩。
    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多