2014-12-06 52 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 
{ 
    ... 
代码