Home > OS >  extract Text from selected nodes
extract Text from selected nodes

Time:01-26

VB Code , XML files.

i can't get the text of the node , a childnode of

XML look like this.

<...>
<OperationsSegment>

                <ID>10</ID>

                <Description>Description_1</Description>

                <Parameter>

                               <ID>Speed_1</ID>

                               <Value>

                                               <ValueString>70</ValueString>

                               </Value>

                </Parameter>

                <Parameter>

                               <ID>Speed_2</ID>

                               <Value>

                                               <ValueString>50</ValueString>

                               </Value>

                </Parameter>

                <Parameter>

                               <ID>Fan_Speed_1</ID>

                               <Value>

                                               <ValueString>30</ValueString>

                               </Value>

                </Parameter>

</OperationsSegment>
<...>

i have this code for now, it is supposed to look for eatch /parameter/value where the ID match the requested ID (speed_1) for example

Dim colNodesValParam
Dim objNodeValParam
Dim xmlDoc
Dim ID_VALUE
 

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.Async = "False"

xmlDoc.Load(MyFile)

 
ID_VALUE = speed_1


Set colNodesValParam = xmlDoc.selectNodes("OperationsSegment/Parameter[ID='" & ID_VALUE & "']/Value")

                                                                              

For Each objNodeValParam In colNodesValParam.Elements       

               
                // get  the value or text of the node “<ValueString>”                                                    

                Texte = objNodeValParam.Text
               

Next

i have ben trying with "objNodeValParam.Value" as well. and other XML post got me nowhere so far exept being able to entre my For Each whitch i couldnt do before.

Edit : i understand why .Value dose not work but still not able to get the text 50; 70 or 30 depanding on my ID_VALUE

CodePudding user response:

This example should help,

    Dim foo As XElement
    ' foo = XElement.Load("path here")
    ' for test use literal

    foo = <OperationsSegment>
              <ID>10</ID>
              <Description>Description_1</Description>
              <Parameter>
                  <ID>Speed_1</ID>
                  <Value>
                      <ValueString>70</ValueString>
                  </Value>
              </Parameter>
              <Parameter>
                  <ID>Speed_2</ID>
                  <Value>
                      <ValueString>50</ValueString>
                  </Value>
              </Parameter>
              <Parameter>
                  <ID>Fan_Speed_1</ID>
                  <Value>
                      <ValueString>30</ValueString>
                  </Value>
              </Parameter>
          </OperationsSegment>

    Dim ie As IEnumerable(Of XElement)
    ie = From el In foo...<Parameter>
             Where el.<ID>.Value = "Speed_1"
             Select el

    For Each el As XElement In ie
        Dim s As String = el.<Value>.<ValueString>.Value
    Next

CodePudding user response:

I made it work by removing .Elements from the For Eatch

from this :

Set colNodesValParam = xmlDoc.selectNodes("OperationsSegment/Parameter[ID='" & ID_VALUE & "']/Value")

                                                                              

For Each objNodeValParam In colNodesValParam.Elements       

to this :

Set colNodesValParam = xmlDoc.selectNodes("OperationsSegment/Parameter[ID='" & ID_VALUE & "']/Value")

                                                                              

For Each objNodeValParam In colNodesValParam     

made the code print the entier into parameters i can now use.

  • Related