1

I have xml that looks like this:

<Doc>
  <Thing>
      <ID type="One">Fred</ID>
   </Thing>
   <Thing>
      <ID>Bill</ID>
   </Thing>
   <Thing>
     <ID type="Two">John</ID>
   </Thing>
</Doc>

I can write LINQ to find the type="One" nodes. (or type="Two")

Dim ThingList As IEnumerable(Of XElement) = _
   From el In doc...<Thing>.<ID> _
   Where el.Attribute("type").Value = "One" _
   Select el

How would I write a linq query to find the ID node that doesn't have a type attribute at all?

Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
dac
  • 57
  • 7

1 Answers1

3

You should use the fact that Attribute() will return null/nothing if there's no such attribute.

In C# I would use:

var idsMissingType = doc.Descendants("ID") // Or whatever
                        .Where(x => x.Attribute("type") == null);

My guess is that the VB you want is:

Dim ThingList As IEnumerable(Of XElement) = _
   From el In doc...<Thing>.<ID> _
   Where el.Attribute("type") Is Nothing _
   Select el
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194