Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Have xml that has repeating node: <STRUS name="AB"
<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="54">

  <SVALUs>

   <SVALU  name="TREE" line="35"><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">TCOWS</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>

Already have code 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 and TCOWS:

Can you edit existing code? Need to extract the values in repeating loop code below:

VB
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) for the node for line 18 where pos = 9 
'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 ValueStr as STring = ????? 
    'First loop returns PCCARE and then second iteration returns TCOWS
Next
Posted
Updated 29-Mar-10 23:02pm
v2

1 solution

Your problem is the node with the attribute "pos" is a child of the SVALU node however you are looking for it at the same level
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900