从XML文件读取到ListView-Windows Phone圣经应用程序

时间:2014-12-06 20:44:00

标签: c# xml windows-phone

我是视觉工作室2012中的Windows手机的新手,我想用我的母语非洲语言建立一个圣经应用程序。我在从一个xml文件中读取书名,章和节并且在ListView中显示时遇到问题。这就是我所做的。请帮忙。

XML FIlE

<?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#CODE

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; }
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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
{
    ...