我重複使用Windows窗體應用程序中使用的代碼。我在這裏找到了一些幫助,以便使用StringReader
類免費獲得我的代碼錯誤。當運行我的應用程序,這是一款Windows Phone Silverlight應用程序,我收到一個異常說XML閱讀器錯誤
數據在根級別是無效的,1線位置1
代碼的目的是利用一個ISBN號碼,搜索該書的isbndb.com併發回標題,作者和描述。任何幫助這個錯誤,將不勝感激。該方法返回memberbook
對象中的「title」,「author」和「description」項。
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;
}
}
}
編輯SampleXML(LB)
<?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>
你的xml文件(包括頭文件)的一個片段將會很有用。 – ChrisF
非常看好,因爲錯誤消息是說它不是很好地形成xml。 –