c# - 使用 linq 读取属性值

标签 c# linq linq-to-xml

我有一个如下所示的 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”的属性值

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

干杯, 克里斯

最佳答案

我怀疑你想要:

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

关于c# - 使用 linq 读取属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/532816/

相关文章:

c# - 没有日历可视化的 ASP.NET Datepicker

c# - ASP.Net 中的 response.redirect 问题

sql - 使用 Entity Framework 4.1 的多表连接,我应该使用 lambda 还是 LINQ?

c# - 获取列表中数字的最高平均值

c# - 遍历 XDocument 中的每个 XElement

sql-server - 如何在 .NET 中进行与 SQL Server 排序规则相同的排序?

c# - Automapper - 将 UTC 转换为特定时区

具有多个聚合的 LINQ 查询

c# - 使用包含 & 符号的 URL 将 XML 加载到 XDocument

c# - 磁贴在 WP8.1 Silverlight 应用程序中未正确显示