Linq, what am I doing wrong?

Code:

Dim strURLSearch As String
strURLSearch = "http://isbndb.com/api/books.xml"
strURLSearch = strURLSearch & "?access_key=XXXXXX"
strURLSearch = strURLSearch & "&index1=isbn&value1=" & Me.txtISBN.Text

Dim xResults As XDocument = XDocument.Load(strURLSearch)




Dim books = From book In xResults.Descendants("BookData") Select ISBN = Me.txtISBN.Text, Title = book.Element("Title").Value, Author = book.Element("AuthorText").Value, Publisher = book.Element("PublisherText").Value

For Each book In books
Me.txtResults.Text = Me.txtResults.Text & book.ISBN & vbTab & book.Title & vbTab & book.Author & vbCrLf

Next

End code

I'm getting a NullReferenceException on the for next loop. Note, I did check the XML that I'm receiving back:


-

-

"A" is for alibi
"A" is for alibi: a Kinsey Millhone Mystery
by Sue Grafton
New York : Bantam Books, 1987, c1982.



Comments

  • Maybe you can use this approach instead:
    [code]
    Dim strURLSearch As String
    strURLSearch = "http://isbndb.com/api/books.xml"
    strURLSearch = strURLSearch & "?access_key=XXXXXXX"
    strURLSearch = strURLSearch & "&index1=isbn&value1=" & Me.txtIsbn.Text

    Dim xResults As XDocument = XDocument.Load(strURLSearch)




    Dim books = From book In xResults.Descendants("BookData") _
    Select book

    For Each b In books
    Console.WriteLine("Found {0}", b.Element("Title").Value)
    .....
    Next
    [/code]
    : Code:
    :
    : Dim strURLSearch As String
    : strURLSearch = "http://isbndb.com/api/books.xml"
    : strURLSearch = strURLSearch & "?access_key=XXXXXX"
    : strURLSearch = strURLSearch & "&index1=isbn&value1=" &
    : Me.txtISBN.Text
    :
    : Dim xResults As XDocument = XDocument.Load(strURLSearch)
    :
    :
    :
    :
    : Dim books = From book In xResults.Descendants("BookData")
    : Select ISBN = Me.txtISBN.Text, Title = book.Element("Title").Value,
    : Author = book.Element("AuthorText").Value, Publisher =
    : book.Element("PublisherText").Value
    :
    : For Each book In books
    : Me.txtResults.Text = Me.txtResults.Text & book.ISBN &
    : vbTab & book.Title & vbTab & book.Author & vbCrLf
    :
    : Next
    :
    : End code
    :
    : I'm getting a NullReferenceException on the for next loop. Note, I
    : did check the XML that I'm receiving back:
    :
    :
    : -
    :
    : -
    :
    : "A" is for alibi
    : "A" is for alibi: a Kinsey Millhone Mystery
    : by Sue Grafton
    : New York : Bantam Books,
    : 1987, c1982.
    :
    :
    :
    :
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion