Extract value In a Loop w/ XLINQ

Hi Experts and thanks for reading this and for your help.

Already have code (thanks to CuteBug) that uses xlinq to LOOP through the repeating node <STRUS name="AB"
but unable to extract particular value from name="ZRT" and pos="9" to get PCCARE (line #7) and TWCOW (line #18):

Can you help edit existing code? Need to extract the values in repeating loop code below:
*it's important that the extraction of the value occurs in the loop

Dim xDoc As XDocument = XDocument.Load("E:\Some.xml")
Dim root As XElement = xDoc.Root

' Get the Node have STRU name="AB"
Dim STElems = From x In root.Descendants("STRUS") _
    Where (x.Attribute("name").Value = "AB") _
    Select x

' Iterate through each AB Node
For Each STElem As XElement In STElems

'How do you extract (PCCARE) and (TWCOW)
'Tried this but know it doesn't work:
    Dim elements9 = From e In STElem.Descendants("SVALU") _
        Where (e.Attribute("pos").Value = "9") And (e.Attribute("name").Value = "ZRT") _
        Select e.Value

    Dim TempStr as String = ????
Next

<STRUS  name="AB" lineStart="3" lineEnd="33">
  <SVALUs>
   <SVALU  name="TREE" line="33"><segment  id="SE" xml:space="preserve"><element  pos="1">34</element><element  pos="2">100550212</element></segment></SVALU>
  </SVALUs>
  <STRUS  name="1000A" lineStart="5" lineEnd="6">
   <SVALUs>
    <SVALU  name="ZRT" line="5"><segment  id="NM1" xml:space="preserve"><element  pos="1">41</element>...<element  pos="9">PCCARE</element></segment></SVALU>
    <SVALU  name="PER" line="6"><segment  id="PER" xml:space="preserve"><element  pos="1">IC</element>...<element  pos="4">12345678</element></segment></SVALU>
   </SVALUs>
  </STRUS>
 </STRUS>
<STRUS  name="AB" lineStart="34" lineEnd="53">
  <SVALUs>
   <SVALU  name="TREE" line="34"><segment  id="SE" xml:space="preserve"><element  pos="1">34</element><element  pos="2">100550212</element></segment></SVALU>
  </SVALUs>
  <STRUS  name="1000A" lineStart="35" lineEnd="36">
   <SVALUs>
    <SVALU  name="ZRT" line="37"><segment  id="NM1" xml:space="preserve"><element  pos="1">41</element>...<element  pos="9">TWCOW</element></segment></SVALU>
    <SVALU  name="PER" line="38"><segment  id="PER" xml:space="preserve"><element  pos="1">IC</element>...<element  pos="4">12345678</element></segment></SVALU>
   </SVALUs>
  </STRUS>
 </STRUS>

Open in new window

LVL 5
allanau20Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CuteBugCommented:
Hi allanau20,

In the XML the attribute "pos" belongs to the "element" tag and not to the "SVALU" tag.

Therefore you need to query for the tag "SVALU" whose attribute "name" = "ZRT"
Once you get that tag, you need to query for the Descendants "elements" whose attribute "pos" has the value "9".

I am not an expert in VB.NET but I hope you can make out the code below

 Dim elementsZRT = From e In STElem.Descendants("SVALU") _
        Where (e.Attribute("name").Value = "ZRT") _
        Select e.Value

' Iterate through each ZRT Node
For Each ZRTelem As XElement In elementsZRT 

Dim elements9 = From el In ZRTelem .Descendants("element") _
        Where (el.Attribute("pos").Value = "9") _
        Select el.Value

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi allanau20;

Below are two code snippets, the difference between them is looping through the result set. if there can only be one element with attribute pos and value of 9 then the second snippet is more efficient.

Fernandio
Dim root As XElement = xDoc.Root

' Get the Node have STRU name="AB"
Dim STElems = From x In root.Descendants("STRUS") _
    Where (x.Attribute("name").Value = "AB") _
    Select x

' Iterate through each AB Node
For Each STElem As XElement In STElems
    ' Query for the element nodes whos attribute pos has a value of 9 and whos Parent, segment, 
    ' Parent, SVALU, has an attribute of name with a value of ZAR
    Dim elements9 = From e In STElem.Descendants("element") _
                    Where e.Parent.Parent.Attribute("name").Value = "ZRT" And _
                          e.Attribute("pos").Value = "9" _
                    Select e.Value

    ' Because the above query returns a collection you will need to iterate through the results.
    For Each result In elements9
        Console.WriteLine(result)
    Next
Next

' If there can only be at most 1 element with attribute post with a vale of 9 in each section 
' then you can do away for the For Each loop to display the info as shown below

Dim root As XElement = xDoc.Root                                                                               
                                                                                                               
' Get the Node have STRU name="AB"                                                                             
Dim STElems = From x In root.Descendants("STRUS") _                                                            
    Where (x.Attribute("name").Value = "AB") _                                                                 
    Select x                                                                                                   
                                                                                                               
' Iterate through each AB Node                                                                                 
For Each STElem As XElement In STElems                                                                         
    Dim elements9 = (From e In STElem.Descendants("element") _                                                 
                     Where e.Parent.Parent.Attribute("name").Value = "ZRT" And _
                           e.Attribute("pos").Value = "9" _
                     Select e.Value).FirstOrDefault()                                                          

    Console.WriteLine(elements9)                                                                               
                                                    
Next                       

Open in new window

0
allanau20Author Commented:
wow ... thank you; I really appreciate your help.
I'll try it once i get in and C# is fine as well. Thank you both!!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

allanau20Author Commented:
Thanks CuteBug:

On this line:
    For Each ZRTelem As XElement In elementsZRT

Tthere's an error on elementsZRT:
    Value of type 'String' cannot be converted to 'System.Xml.Linq.XElement'.

I'll try yours next FernandoSoto.
0
allanau20Author Commented:
you guys are super!! I've been googling and the examples out there are for nice structured xml and was pulling my hair with this one. Wish I could give each of u 500 pts; but the best I can do is split.  Thanks again.
0
allanau20Author Commented:
Super!
0
Fernando SotoRetiredCommented:
Not a problem, glad to help.  ;=)
0
allanau20Author Commented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.