I’m trying to learn LINQ and have a question on querying XML using VB.
My XML:
<Procedure-Text> <A ID='marker1'></A>Do This Procedure </Procedure-Text> <Procedure-Text> <A ID='marker2'></A>Do That Procedure </Procedure-Text>
How can I specify my query to get only the procedure text that has the ID attribute marker2? In other words, I would like a resulting string that says Do That Procedure.
Thanks
Use VB XML literals:
The triple dot syntax produce ‘all descendants’ of an xml element, i.e.
data...<Procedure-Test>will produce a list of<Procedure-Test>tags insidedataThe dot syntax on XML literals means ‘first descendant’ so
x.<A>will produce the first occurence of<A>inside x. Of which x is now instance of<Procedure-Test>And now that you have the desired
<A>element, comparing its id to a string is trivial with the@attrattribute selector.<A>.@ID = 'marker2'will evaluate toTrueif the ID attribute of the<A>tag is equal to ‘marker2’So
x.<A>.@IDmeans ‘The ID attribute of the first<A>tag inside x’And you want the
<Procedure-Text>element, so you specifySelect xFull example: