Get processing instructions from XML file

the base type for LINQ is the IEnumerable. Have a look here:

@Dave gave us a good explanation on what it is here:

The nature of Where is returning us an IEnumerable(Of T) (just simplified) and can contain:

  • 0
  • 1
  • multiple

So we have to ensure in your case, that we catch the data from 1 and only one element (think on no filter return and/or multiple catches).

For this we can work with DefaultIfEmpty and mixing Method and Query Syntax:

(From pi In xDoc.Nodes().Where(Function (x)  x.NodeType = XmlNodeType.ProcessingInstruction).Cast(Of XProcessingInstruction)
Where pi.Target.Equals(PIFilterKey)
Select pi).DefaultIfEmpty(New XProcessingInstruction("NoName","UNDEFINED")).First().Data

line1: fetch all PIs
line2: Filter all PIs with the name equal to the FilterKey
line3: for an empty result return a dummy PI, other wise return the found PIs, from this take the first PI and its Data

In case of we would expect multiple PI as a filer result we would return it e.g. as List/Array and process / evaluate later.

Output when found:

Output empty filter result: