I am reusing code used in a Windows forms application. I found some help on here to get my code error free using the StringReader class. When running my application, which is a Windows Phone Silverlight application, I receive an exception saying
Data at the root level is invalid, Line 1 Position 1
The purpose of the code is to use an ISBN number, search isbndb.com for the book and send back the title, author and description. Any help on this error would be appreciated. The method returns the items “title”, “author” and “description” in a memberbook object.
public class ISBNDB
{
public string key = "??????????";
public string URL = "http://isbndb.com/api/books.xml?access_key=";
string DETAILS;
private MemberBook mb;
private string title;
private string author;
private string description;
public ISBNDB()
{
URL += key;
DETAILS += key;
title = null;
}
public ISBNDB(string key)
{
this.key = key;
URL += key;
DETAILS += key;
title = null;
}
public MemberBook GetData(string isbn)
{
DETAILS = URL;
DETAILS += "&results=texts&index1=isbn&value1=" + isbn;
using (XmlReader reader = XmlReader.Create(new StringReader(DETAILS)))
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
switch(reader.Name)
{
case "Title":title = reader.ReadContentAsString();
break;
case "AuthorsText": author = reader.ReadContentAsString();
break;
case "Summary": description = reader.ReadContentAsString();
if (description.Equals(""))
description = "Not Available";
if(description.Length > 2000)
description = "Not Available";
break;
}
break;
}
}
return mb;
}
}
}
EDIT SampleXML (L.B)
<?xml version="1.0" encoding="UTF-8"?>
<ISBNdb server_time="2012-01-26T22:30:26Z">
<BookList total_results="1" page_size="10" page_number="1" shown_results="1">
<BookData book_id="jaws_a05" isbn="1400064562" isbn13="9781400064564">
<Title>Jaws</Title>
<TitleLong></TitleLong>
<AuthorsText>Peter Benchley, </AuthorsText>
<PublisherText publisher_id="random_house">Random House</PublisherText>
<Summary>"Relentless terror." The Philadelphia Inquirer.The classic, blockbuster thriller of man-eating terror that inspired the Steven Spielberg movie and made millions of beachgoers afraid to go into the water. Experience the thrill of helpless horror again -- or for the first time!From the Paperback edition.</Summary>
<Notes></Notes>
<UrlsText></UrlsText>
<AwardsText></AwardsText>
</BookData>
</BookList>
</ISBNdb>
You don’t want to use synchronous network calls in Silverlight. Instead, use an async call to get the XML as a string. Then in the callback, parse it as XML. To get the string asynchronously, use something like this: