Link to home
Start Free TrialLog in
Avatar of soriega
soriegaFlag for Sweden

asked on

XML in VB.NET

Hi. i am trying to read some XML that comes from my stockbroker, but i don't get the exact result i wan't from the examplecode

This is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<securityTrades type="array">
  <securityTrade>
    <securityTrade>
      <counterparty>MCF</counterparty>
      <volume>500</volume>
      <tradeID>B277249-20110706</tradeID>
      <tradetime>17:24:41</tradetime>
      <instrumentID>
        <marketID>11</marketID>
        <identifier>5095</identifier>
      </instrumentID>
      <orderID>2049989</orderID>
      <price>
        <value>45.77</value>
        <curr>SEK</curr>
      </price>
      <accno>1903351</accno>
      <side>BUY</side>
    </securityTrade>
  </securityTrade>
  <securityTrade>
    <securityTrade>
      <counterparty>MCF</counterparty>
      <volume>500</volume>
      <tradeID>B283773-20110706</tradeID>
      <tradetime>17:29:52</tradetime>
      <instrumentID>
        <marketID>11</marketID>
        <identifier>5095</identifier>
      </instrumentID>
      <orderID>2050014</orderID>
      <price>
        <value>45.67</value>
        <curr>SEK</curr>
      </price>
      <accno>1903351</accno>
      <side>BUY</side>
    </securityTrade>
  </securityTrade>
</securityTrades>




The code for parsing the xmlstring is this, however at "Next SECTR" i get a "nullreferenceexception", does anyone have a quick workaround to this? I think its fairly easy but i am not very used to XML

I send the XMLstring into the function:


    Function ParseTradedOrderID(ByVal xmlresponse As String) As Long
        If xmlresponse = "" Then
            Return 0
            Exit Function
        End If

        If Len(xmlresponse) < 75 Then
            Return 0
            Exit Function
        End If

        Debug.Print(xmlresponse)
        'xmlresponse = Replace(xmlresponse, " ", "")
        'xmlresponse = Replace(xmlresponse, "<securityTrade> <securityTrade>", "<securityTrade>")
        'Debug.Print(xmlresponse)
        'xmlresponse = Replace(xmlresponse, vbCrLf, "")
        'Dim ac As String = Trim(xmlresponse)
        'Debug.Print(ac)
        'Dim a As Object
        'a = Split(xmlresponse, "><")


        Dim document As XDocument = XDocument.Parse(xmlresponse)
        Dim securityTrades = From securityTrade In document.Descendants("securityTrade") _
                          Select New With _
        { _
            .volume = securityTrade.Element("securityTrade").Element("volume").Value, _
            .orderID = securityTrade.Element("securityTrade").Element("orderID").Value, _
             .price = securityTrade.Element("securityTrade").Element("price").Element("value").Value, _
        .side = securityTrade.Element("securityTrade").Element("side").Value _
        }


        Debug.Print(xmlresponse)
        '  Debug.Print(securityTrades.)

        For Each SECTR In securityTrades
            Debug.Print(SECTR.orderID)

        Next SECTR


    End Function
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of soriega

ASKER

Thanks, very quick and very accurate!
NP. Glad to help  = )