[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

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

0
allanau20
Asked:
allanau20
  • 5
  • 2
2 Solutions
 
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now