R
Roshawn
Hi,
My usage of Linq to Xml has been going great until now. I'm getting an error whenever I
don't find a particular node in the xml, even though I use an auxiliary function to test
for it. Here's what my code looks like (note that I'm using Amazon Web Services):
Imports <xmlns="http://webservices.amazon.com/AWSECommerceService/2007-06-13">
....
....
....
Private Sub GetAmazonStuff()
'code to actually call Amazon Web Services omitted
Dim MyNodes = (From nd In doc...<Item> _
Select New Item With {.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value, _
.ListPrice = TestNode(nd.<ItemAttributes>.<ListPrice>), _
.OurPrice = TestNode(nd.<Offers>.<Offer>.<OfferListing>.<Price>)})
bindingSource.DataSource = MyNodes.ToList() 'error always occurs here & points to MyNodes
Me.dgvAmazon.DataSource = bindingSource
End Sub
Private Function TestNode(ByVal element As XElement) As String
If Not (element) Is Nothing Then
Return element.<FormattedPrice>.Value
Else
Return "no data"
End If
End Function
When running this code, I always get the "Object not set to an instance of an object"
error directed at the MyNodes variable. Code execution stops (as indicated above) despite
there being other nodes that haven't been evaluated. Debugging revealed that the error is
thrown whenever the TestNode function finds that the element Is Nothing, which means it
doesn't exist.
Why does that happen?
Thanks,
Roshawn
My usage of Linq to Xml has been going great until now. I'm getting an error whenever I
don't find a particular node in the xml, even though I use an auxiliary function to test
for it. Here's what my code looks like (note that I'm using Amazon Web Services):
Imports <xmlns="http://webservices.amazon.com/AWSECommerceService/2007-06-13">
....
....
....
Private Sub GetAmazonStuff()
'code to actually call Amazon Web Services omitted
Dim MyNodes = (From nd In doc...<Item> _
Select New Item With {.ASIN = nd.<ASIN>.Value, _
.Image = nd.<SmallImage>.<URL>.Value, _
.Title = nd.<ItemAttributes>.<Title>.Value, _
.ListPrice = TestNode(nd.<ItemAttributes>.<ListPrice>), _
.OurPrice = TestNode(nd.<Offers>.<Offer>.<OfferListing>.<Price>)})
bindingSource.DataSource = MyNodes.ToList() 'error always occurs here & points to MyNodes
Me.dgvAmazon.DataSource = bindingSource
End Sub
Private Function TestNode(ByVal element As XElement) As String
If Not (element) Is Nothing Then
Return element.<FormattedPrice>.Value
Else
Return "no data"
End If
End Function
When running this code, I always get the "Object not set to an instance of an object"
error directed at the MyNodes variable. Code execution stops (as indicated above) despite
there being other nodes that haven't been evaluated. Debugging revealed that the error is
thrown whenever the TestNode function finds that the element Is Nothing, which means it
doesn't exist.
Why does that happen?
Thanks,
Roshawn