Чтение из XML-файла в ListView - приложение для чтения Windows Phone

1

Я новичок в телефоне Windows в visual studio 2012, и мне хотелось бы создать библейское приложение на моем родном африканском языке. У меня возникла проблема с чтением заголовка книги, главы и стиха из файла 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>

С# 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; }
            }
        }
    }
}
Теги:
windows-phone

1 ответ

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
{
    ...

Ещё вопросы

Сообщество Overcoder
Наверх
Меню