Quick LINQ to XML Query Question

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am hoping this is actually fairly simple, but digging to the documentation and the samples I couldnt see anything



so if I have an xmldoc like

<pre class="prettyprint <?xml version="1.0" encoding="utf-8"?>
<A>
<Qs>
<Q Text="A Sample 01
<Ts>
<T Text="ABC" />
<T Text="123" />
</Ts>
</Q>
<Q Text="A Sample 02
<Ts>
<T Text="ABC" />
<T Text="345" />
</Ts>
</Q>
<Q Text="A Sample 03
<Ts>
<T Text="XYZ" />
</Ts>
</Q>
</Qs>
</A>[/code]
Im trying to write a LINQ to XML query that would give me all Q elements that have a Descendent T with a Text value of "ABC"


<pre class="prettyprint var queryresults = from a in xmlfile1.Descendents("Q")
WHERE (String) a.Descendants("T").Attribute("Text") = "ABC"
SELECT a [/code]
Which doesnt work, I have been digging through the samples to see if I can find any that show something similar to what Im trying to do, but so far no luck. Is there a easy way go about this? I mean I guess I could pull all
the Q into a var and then do a foreach() and search through the descendants for a T with the right text, but I thought there might be an easier way.


Thanks in advance.

View the full article
 
Back
Top