soriega
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</counter party>
<volume>500</volume>
<tradeID>B277249-20110706< /tradeID>
<tradetime>17:24:41</trade time>
<instrumentID>
<marketID>11</marketID>
<identifier>5095</identifi er>
</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</counter party>
<volume>500</volume>
<tradeID>B283773-20110706< /tradeID>
<tradetime>17:29:52</trade time>
<instrumentID>
<marketID>11</marketID>
<identifier>5095</identifi er>
</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(xmlrespons e)
Dim securityTrades = From securityTrade In document.Descendants("secu rityTrade" ) _
Select New With _
{ _
.volume = securityTrade.Element("sec urityTrade ").Element ("volume") .Value, _
.orderID = securityTrade.Element("sec urityTrade ").Element ("orderID" ).Value, _
.price = securityTrade.Element("sec urityTrade ").Element ("price"). Element("v alue").Val ue, _
.side = securityTrade.Element("sec urityTrade ").Element ("side").V alue _
}
Debug.Print(xmlresponse)
' Debug.Print(securityTrades .)
For Each SECTR In securityTrades
Debug.Print(SECTR.orderID)
Next SECTR
End Function
This is the XML:
<?xml version="1.0" encoding="UTF-8"?>
<securityTrades type="array">
<securityTrade>
<securityTrade>
<counterparty>MCF</counter
<volume>500</volume>
<tradeID>B277249-20110706<
<tradetime>17:24:41</trade
<instrumentID>
<marketID>11</marketID>
<identifier>5095</identifi
</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</counter
<volume>500</volume>
<tradeID>B283773-20110706<
<tradetime>17:29:52</trade
<instrumentID>
<marketID>11</marketID>
<identifier>5095</identifi
</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(xmlrespons
Dim securityTrades = From securityTrade In document.Descendants("secu
Select New With _
{ _
.volume = securityTrade.Element("sec
.orderID = securityTrade.Element("sec
.price = securityTrade.Element("sec
.side = securityTrade.Element("sec
}
Debug.Print(xmlresponse)
' Debug.Print(securityTrades
For Each SECTR In securityTrades
Debug.Print(SECTR.orderID)
Next SECTR
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
NP. Glad to help = )
ASKER