Click here to Skip to main content
15,892,839 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I am new to windows phone in visual studio 2012 and I would like to build a bible app in my native african language. I'm having a problem with reading Book Title, Chapter and verse from an xml file. Here is what I have done. Please help.
XML FIlE
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# CODE

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; }
            }
        }
}
Posted
Comments
BillWoodruff 7-Dec-14 1:35am    
First, what is the problem you are having ? Do you get error messages: if so, what are they, and where do they occur. If you don't get error messages, but the result of your code is not what you want, then please post an example of the output you get now, and describe what's not right with it.

I wonder if a solution I wrote for another recent question here that involved parsing Xml using XDocument could assist you:

http://www.codeproject.com/Answers/849574/Can-I-Sort-Results-Of-List

Is the structure you show in your Xml example, used consistently for all content in your source Xml ?
Member 10321418 7-Dec-14 14:56pm    
Hi BillWoodruff
No errors displayed and it doesn't display any items in the listbox. I tried to do it the other way round using the code below, but now it only displays the first verse and ignores the second verse.
var data = from query in myData.Descendants("bible")
select new Bible
{
Book = (string)query.Element("book"),
Chapter = (string)query.Element("chapter"),
Verse = (string)query.Element("verse"),
};

1 solution

Try this:
C#
var verses = from v in xdoc.Elements("bible").Elements("testament").Elements("book").Elements("chapter").Elements("verse")
        select new {
            VerseId = v.Attribute("number").Value,
            VerseText = v.Value
            };


It should return:
VerseId VerseText
1       In the beginning God created the heaven and the earth.
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.


I have no idea how to load data into your BibleLoad class. In my opinion, you need to redesign it, to be able to store the collection of books, chapters and verses.
Please, check it: Walkthrough: Creating Your Own Collection Class[^]
 
Share this answer
 
Comments
Member 10321418 7-Dec-14 16:53pm    
Thanks Maciej Los. It worked and I will now work out the rest.
Maciej Los 8-Dec-14 2:11am    
You're very welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900