Reading attribute values ​​using linq

I have an xml file that looks like this. I am trying to create a query that selects only elements 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 cannot get the value inside the Item, only the Value attribute is of type

Can someone please help me with this?

Cheers, Chris

+3
source share
1 answer

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

Source: https://habr.com/ru/post/1702977/


All Articles