Reputation: 354
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>
Upvotes: 1
Views: 2596
Reputation: 5530
The problem with your XML-Document is probably that there is an PreAmble at the start of your XML-Document. A PreAmble defines which Encoding the following Content is using. Whether this is UTF-8 or UTF-16 or something else.
Inspect your string data (data) that contains the XML-Document with "data[0]" if the output is something other than "<" then there is a PreAmble.
You could then remove the first char of the string while the first char != '<'. If you want to have a look at some different PreAmbles see:
Encoding.UTF8.GetPreAmble()
This will return a byte[] that is used for the PreAmble of an UTF8 FileContent.
Upvotes: 1
Reputation: 116138
First of all, with XmlReader reader = XmlReader.Create(new StringReader(DETAILS))
you will only get your url back since StringReader forms a string from its input. It doesn't download the content of the url.
You can use
var xdoc = XDocument.Load(DETAILS);
or
var xmlReader = XmlReader.Create(DETAILS);
XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(xmlReader);
to get your xml and parse as
xdoc.Descendants("Title").First().Value
Here is a full sample code:
var xdoc = XDocument.Load(DETAILS);
var info = xdoc
.Descendants("BookData")
.Select(n =>
new{
Title = n.Element("Title").Value,
AuthorsText = n.Element("AuthorsText").Value,
Summary = n.Element("Summary").Value,
}
).ToList();
Upvotes: 1
Reputation: 41648
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:
WebClient client = new WebClient();
client.DownloadStringAsync(new Uri(DETAILS));
client.DownloadStringCompleted += OnDownloadComplete;
Upvotes: 0