VB.NET and XmlElement() How do I get the value of the elements out of an API call?

618 Views Asked by At

I have a successful API call and return of data in an XmlElement however I am not able to find out how to parse out the data I need.

Dim Any = CardInqResponse.CardInqRec.Custom.Any <-- This is my xmlelement.

My return shows Length of two (two Elements).
My element names are Exp and Offset. I am interested in the data in Offset where I can see these values returned in innerText and innerXml.

How do I get the value of either of these two to store in a string?

2

There are 2 best solutions below

0
On BEST ANSWER
       Dim offset As String           
       Dim Any =  CardInqResponse.CardInqRec.Custom.Any

       For Each node As XmlElement In Any
           If node.Name = "Offset" Then
            Try
                offset = node.InnerText.ToString
            Catch
            End Try
          End If              
        Next
0
On

Load API response into XmlDocument and select a node with xpath and get innertext of selected node. below example is for an idea

    Dim doc = new XmlDocument()
    doc.LoadXml(yourXmlString)
    txtStreet.Text = doc.SelectSingleNode("/a:Address/a:strStreet", nsm).InnerText

For more info refer https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument?redirectedfrom=MSDN&view=netcore-3.1

Moreover you can share you response then i can see how can we get your required data.