2014-12-06 49 views
1

我是新來的Windows Phone在Visual Studio 2012中,我想用我的非洲土着語言來構建一個聖經應用程序。我在閱讀書名,章節和詩歌時遇到了問題,並在ListView中顯示。這是我所做的。請幫忙。從XML文件閱讀到ListView- Windows Phone聖經App

XML文件

<?xml version="1.0" encoding="utf-8" ?> 

<bible translation="King James Version"> 
    <testament name="Old"> 
    <book name="Genesis"> 
     <chapter number="1"> 
     <verse number="1">In the beginning God created the heaven and the earth.</verse> 
     <verse number="2">And the earth was without form, and void; and darkness was upon the face of  the deep. And the Spirit of God moved upon the face of the waters.</verse> 
     </chapter> 
    </book> 
    </testament> 
</bible> 

C#代碼

namespace BibleApp 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 

      XDocument loadedData = XDocument.Load("Bible.xml"); 
      var SearchData = from c in loadedData.Descendants("testament").Descendants("book").Descendants("chapter").Descendants("verse") 
          where (bool)c.Parent.Parent.Parent.Attribute("name") 
          where (bool)c.Parent.Parent.Attribute("name") 
          where (bool)c.Parent.Attribute("number") 
          select new BibleLoad 
          { 
           VerseNumber = (string)c.Attribute("number"), 
           BibleText = (string)c.Value.ToString(), 
           TheFontSize = FontSize 
          }; 
      TheList.ItemsSource = SearchData; 
      TheList.Visibility = Visibility.Visible; 

      // Sample code to localize the ApplicationBar 
      //BuildLocalizedApplicationBar(); 
     } 

     public class BibleLoad 
     {  
      string myTestament; 
      string myBook; 
      string myChapter; 
      string myVerse; 
      double fontSize; 
      string bibleText; 

      public string Testament 
      { 
       get { return myTestament; } 
       set { myTestament = value; } 
      } 

      public string Book 
      { 
       get { return myBook; } 
       set { myBook = value; } 
      } 

      public string Chapter 
      { 
       get { return myChapter; } 
       set { myChapter = value; } 
      } 

      public string VerseNumber 
      { 
       get { return myVerse; } 
       set { myVerse = value; } 
      } 

      public double TheFontSize 
      { 
       get { return fontSize; } 
       set { fontSize = value; } 
      } 

      public string BibleText 
      { 
       get { return bibleText; } 
       set { bibleText = value; } 
      } 
     } 
    } 
} 

回答

0

where clauses拋出異常,因爲你不能值"Old", "Genesis" or "1"轉換爲布爾。

... 
where (bool)c.Parent.Parent.Parent.Attribute("name") //throw exception! 
where (bool)c.Parent.Parent.Attribute("name") 
where (bool)c.Parent.Attribute("number") 
select new BibleLoad 
{ 
    ... 

如果你的意思是過濾那些具有這些屬性的元素,你可以這樣寫

... 
where ((c.Parent.Parent.Parent.Attribute("name") != null) && 
     (c.Parent.Parent.Attribute("name")!= null) && 
     (c.Parent.Attribute("number") != null)) 
select new BibleLoad 
{ 
    ... 
代碼