2012-01-26 120 views
0

我重複使用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> 
+5

你的xml文件(包括頭文件)的一個片段將會很有用。 – ChrisF

+0

非常看好,因爲錯誤消息是說它不是很好地形成xml。 –

回答

0

你不希望使用Silverlight中的同步網絡調用。相反,使用異步調用將XML作爲字符串來獲取。然後在回調中,將其解析爲XML。爲了得到字符串異步,使用這樣的:

WebClient client = new WebClient(); 
client.DownloadStringAsync(new Uri(DETAILS)); 
client.DownloadStringCompleted += OnDownloadComplete; 
1

首先,用XmlReader reader = XmlReader.Create(new StringReader(DETAILS))你只會得到您的網址後面,因爲StringReader形成從字符串其輸入。它不會下載url的內容。

您可以使用

var xdoc = XDocument.Load(DETAILS); 

var xmlReader = XmlReader.Create(DETAILS); 
XmlDocument xmlDoc = new System.Xml.XmlDocument(); 
xmlDoc.Load(xmlReader); 

讓你的XML和解析爲

xdoc.Descendants("Title").First().Value


她e是一個完整的示例代碼:

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(); 
+0

同步XDocument.Load調用會讓你陷入麻煩嗎?無論如何,對於精細的LINQ to XML示例,+1。 –

1

問題與XML的文檔可能有你的XML的文檔的開頭的序言。 PreAmble定義了下列內容正在使用的編碼。無論這是UTF-8還是UTF-16或其他。

如果輸出不是「<」,那麼檢查包含帶有「data [0]」的XML文檔的字符串數據(數據),然後出現PreAmble。

然後,您可以刪除字符串的第一個字符,而第一個字符!='<'。 如果你想看看一些不同的前導看到:

Encoding.UTF8.GetPreAmble() 

這將返回一個byte []用於爲UTF8 FileContent的序言。