Deserialize XML into class object

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi all:
Im sorry as this may be a simple question, however looking through the various links I could not find a solution to my problem.
I am trying to deserialize XML I receive from a website and display the info. Im not 100% familair how XML works other than very simple object. This is a sample of the XML I receive from the site:
<?xml version="1.0" encoding="UTF-8"?><br/>
<searchResults><br/>
<resultCount>95</resultCount><br/>
<Book><br/>
<bookId>4343589241</bookId><br/>
<isbn10>0451161351</isbn10><br/>
<isbn13>9780451161352</isbn13><br/>
<listingCondition>NOT NEW BOOK</listingCondition><br/>
<itemCondition>Good</itemCondition><br/>
<quantity>3</quantity><br/>
<vendorCurrency>USD</vendorCurrency><br/>
<listingPrice>1.0</listingPrice><br/>
<firstBookShipCost>2.95</firstBookShipCost><br/>
<extraBookShipCost>0.0</extraBookShipCost><br/>
<minShipDays>0</minShipDays><br/>
<maxShipDays>0</maxShipDays><br/>
<totalListingPrice>3.95</totalListingPrice><br/>
<listingUrl>www.abebooks.com/servlet/BookDetailsPL?bi=4343589241&amp;cm_ven=sws&amp;cm_cat=sws&amp;cm_pla=sws&amp;cm_ite=4343589241</listingUrl><br/>
<author>Stephen King</author><br/>
<title>Cujo</title><br/>
<publisherName>Signet (MM)</publisherName><br/>
<catalogImage>http://isbn.abebooks.com/mz/51/45/0451161351.jpg</catalogImage><br/>
<vendorName>ThriftBooks</vendorName><br/>
<vendorLocation>Auburn, WA, U.S.A.</vendorLocation><br/>
<vendorId>54239181</vendorId><br/>
<sellerRating>5</sellerRating><br/>
<bindingType>S</bindingType><br/>
</Book><br/>
<Book><br/>
<bookId>6154514099</bookId><br/>
<isbn10>0451161351</isbn10><br/>
<isbn13>9780451161352</isbn13><br/>
<listingCondition>NOT NEW BOOK</listingCondition><br/>
<itemCondition>Good</itemCondition><br/>
<quantity>1</quantity><br/>
<vendorCurrency>USD</vendorCurrency><br/>
<listingPrice>1.0</listingPrice><br/>
<firstBookShipCost>2.8</firstBookShipCost><br/>
<extraBookShipCost>0.0</extraBookShipCost><br/>
<minShipDays>0</minShipDays><br/>
<maxShipDays>0</maxShipDays><br/>
<totalListingPrice>3.80</totalListingPrice><br/>
<listingUrl>www.abebooks.com/servlet/BookDetailsPL?bi=6154514099&amp;cm_ven=sws&amp;cm_cat=sws&amp;cm_pla=sws&amp;cm_ite=6154514099</listingUrl><br/>
<author>King, Stephen</author><br/>
<title>Cujo</title><br/>
<publisherName>Signet</publisherName><br/>
<catalogImage>http://isbn.abebooks.com/mz/51/45/0451161351.jpg</catalogImage><br/>
<vendorName>Break Time Books</vendorName><br/>
<vendorLocation>Elgin, IL, U.S.A.</vendorLocation><br/>
<vendorId>54269886</vendorId><br/>
<sellerRating>5</sellerRating><br/>
<subjects><br/>
<subject>Fiction|Action &amp; Adventure|Psychological</subject><br/>
<subject>Fiction|Mystery &amp; Detective|Suspense</subject><br/>
</subjects><br/>
<bindingType>S</bindingType><br/>
</Book><br/>
<Book><br/>
<bookId>3136015627</bookId><br/>
<isbn10>0451161351</isbn10><br/>
<isbn13>9780451161352</isbn13><br/>
<listingCondition>NOT NEW BOOK</listingCondition><br/>
<itemCondition>Good</itemCondition>
<quantity>4</quantity><br/>
<vendorCurrency>USD</vendorCurrency><br/>
<listingPrice>1.0</listingPrice><br/>
<firstBookShipCost>2.95</firstBookShipCost><br/>
<extraBookShipCost>0.0</extraBookShipCost><br/>
<minShipDays>0</minShipDays><br/>
<maxShipDays>0</maxShipDays><br/>
<totalListingPrice>3.95</totalListingPrice><br/>
<listingUrl>www.abebooks.com/servlet/BookDetailsPL?bi=3136015627&amp;cm_ven=sws&amp;cm_cat=sws&amp;cm_pla=sws&amp;cm_ite=3136015627</listingUrl><br/>
<author>Stephen King</author><br/>
<title>Cujo</title><br/>
<publisherName>Signet (MM)</publisherName><br/>
<catalogImage>http://isbn.abebooks.com/mz/51/45/0451161351.jpg</catalogImage><br/>
<vendorName>Blue Cloud Books</vendorName><br/>
<vendorLocation>Phoenix, AZ, U.S.A.</vendorLocation><br/>
<vendorId>55521360</vendorId><br/>
<sellerRating>5</sellerRating><br/>
<bindingType>S</bindingType><br/>
</Book><br/>
<Book><br/>
<bookId>6557267724</bookId><br/>
<isbn10>0451161351</isbn10><br/>
<isbn13>9780451161352</isbn13><br/>
<listingCondition>NOT NEW BOOK</listingCondition><br/>
<itemCondition>Good</itemCondition><br/>
<quantity>1</quantity><br/>
<vendorCurrency>USD</vendorCurrency><br/>
<listingPrice>1.0</listingPrice><br/>
<firstBookShipCost>2.62</firstBookShipCost><br/>
<extraBookShipCost>0.0</extraBookShipCost><br/>
<minShipDays>0</minShipDays><br/>
<maxShipDays>0</maxShipDays><br/>
<totalListingPrice>3.62</totalListingPrice><br/>
<listingUrl>www.abebooks.com/servlet/BookDetailsPL?bi=6557267724&amp;cm_ven=sws&amp;cm_cat=sws&amp;cm_pla=sws&amp;cm_ite=6557267724</listingUrl><br/>
<author>Stephen King</author><br/>
<title>Cujo</title><br/>
<publisherName>Signet</publisherName><br/>
<catalogImage>http://isbn.abebooks.com/mz/51/45/0451161351.jpg</catalogImage><br/>
<vendorName>OwlsBooks</vendorName><br/>
<vendorLocation>Hammond, IN, U.S.A.</vendorLocation><br/>
<vendorId>51218811</vendorId><br/>
<sellerRating>5</sellerRating><br/>
<subjects><br/>
<subject>Fiction|Action &amp; Adventure|Psychological</subject><br/>
<subject>Fiction|Mystery &amp; Detective|Suspense</subject><br/>
</subjects><br/>
<bindingType>S</bindingType><br/>
</Book><br/>
</searchResults><br/>

I created a class object like this:
Imports System.Xml.Serialization<br/>
Public Class SearchResults<br/>
Public Property resultCount As Integer<br/>
Public Property Books() As New List(Of Book)<br/>
End Class
Public Class Book<br/>
Public Property bookID As String<br/>
Public Property isbn10 As String<br/>
Public Property isbn13 As String<br/>
Public Property listingCondition As String<br/>
Public Property quantity As Integer<br/>
Public Property vendorCurrency As String<br/>
Public Property listingPrice As Double<br/>
Public Property firstBookShipCost As Double<br/>
Public Property extraBooKShipCost As Double<br/>
Public Property minShipDays As Integer<br/>
Public Property maxShipDays As Integer<br/>
Public Property totalListingPrice As Double<br/>
Public Property listingURL As String<br/>
Public Property author As String<br/>
Public Property title As String<br/>
Public Property publisherName As String<br/>
Public Property catalogImage As String<br/>
Public Property vendorName As String<br/>
Public Property vendorLocation As String<br/>
Public Property vendorID As String<br/>
Public Property sellerRating As Integer<br/>
Public Property bindingType As String<br/>
End Class
And I added deserialization logic like this:
Dim response As WebResponse = request.GetResponse()<br/>
Dim data As Stream = response.GetResponseStream<br/>
Dim stream As New StreamReader(data)
Dim reader As XmlTextReader = New XmlTextReader(stream)
Dim books As SearchResults<br/>
Dim xmlSerializer As XmlSerializer = New XmlSerializer(GetType(SearchResults))<br/>
books = CType(xmlSerializer.Deserialize(stream), searchResults)
I receive an error There is an error in XML document(1,1) on the books = CType... line and not sure where the problem is. The XML looks just fine and I can run through the elements using other methods.
I have also attempted to create an XSD and class using the XSD utility and that did not work.
I can deserialize simple single node XML files, however Im getting messed up with resultCount and the nested Books.
Can someone take a peek at the XML and my example code to see what I need to do?


View the full article
 
Back
Top